Jump to content
roninator2

Call Scene Method from a Class

Recommended Posts

Working on a script for someone and I have everything except one part.

When doing evented battles (event command start battle - command_301), it calls Game_Interpreter::command_301

Well for the function of my script I need to make command_301 call a method in Scene_Map.

I just learned about self. for methods for this instance (knew about them before but not for calling a method in a class, I knew about them only for modules)

When I try this it doesn't access the same data. in the new method 'self.method_name' in Scene_Map, I put in a call to 'actual_method', but it always says it cannot find that method.

 

So is there a good way to do this?

Share this post


Link to post
Share on other sites

I am kind of not sure what you want

You can always call a method in the current scene instance by using SceneManager.scene.method_name

But it sound like you want to create a whatchumacallit ('static method' works but I think it's called something different in ruby) in Scene_Map for some reason?

like:

class A_Class

  def self.method_name
    puts "blah"
  end

end

Remember though that methods like that are for the class object it's self not it's instances so...

class A_Class

  def self.a_static_method
    puts "blah"
  end
  
  def a_instance_method
    puts "blah"
  end

end

A_Class.a_static_method # blah
A_Class.a_instance_method # no method found
instance = A_Class.new
instance.a_static_method # no method found
instance.a_instance_method # blah

Likewise:

class A_Class

  def self.a_static_method
    a_instance_method # no method found
  end
  
  def a_instance_method
    a_static_method # no method found
  end

end

 

Edited by Kayzee

Share this post


Link to post
Share on other sites

Right got it. I was trying to do something like SceneManager.Scene_Map.method

But after realizing that I'm currently in Scene_Map when the command 301 is called then I can use the statement you said. SceneManager.scene.method

Otherwise is there a way to call a method from another class.

You said instance = this_class.new

but that makes a new instance so you would not be able to affect variables in the original just the new instance, correct?

 

Share this post


Link to post
Share on other sites
Posted (edited)

Right, a new instance creates a new object and has it's own set of variables. But I am sort of confused, because when you say you want to call a method from another class, what object/instance do you want to run the method? I mean technically I think it may be possible to do some deep magic trickery to make one object run a method of another class as if it was it's own (therefore having it's own variables be effected), but it's something best avoided.

 

Edited by Kayzee

Share this post


Link to post
Share on other sites

Well, take any class in RPG Maker VX Ace, then make it call any method in any scene. but that method in that scene HAS to affect the variables already in that scene.

Understand what I mean now?

It just seems like a flaw in ruby that you can't call a method from another location that has nothing to do with that method, just because you need something in another class or scene to make a change or update in a completely different class or scene. The best example of this is Game_Variables. But not everything runs off of variables. Being able to affect class variables or instance variables would be useful.

Share this post


Link to post
Share on other sites

Well you know each time the scene changes it creates a new instance of that scene right?  It doesn't remember any data about a scene that isn't active.

I mean you can for sure call a method on an object from another object. That much is trivial. All you have to do is keep a reference to that object around somewhere. An instance of Game_Variables is always kept around in the global variable $game_variables so you always have a handy reference to it. SceneManager.scene gives you a reference to the current scene. Also you can actually use class_variable_get and instance_variable_get if you want a sneaky peak at class and instance variables if you want.

 

Share this post


Link to post
Share on other sites

upon examination of SceneManager I see what you mean. So, the only scene you could probably affect is Scene_Map, since it would be loaded and you normally return to Scene_Map not goto. That makes it bad coding then if someone was to use goto Scene_Map instead of return from like the menu or something. You could get duplicate map scenes in the stack.

Thanks for making me go look.

My script is working because when the command 301 is called the person would be on the map talking to an NPC or an event is running so using SceneManager.scene.method works fine.

And I needed to call the method because it's in the map scene where I make a command to the instance variable to do a method (dispose).

Here's the entire script https://forums.rpgmakerweb.com/index.php?threads/panic-window.143427/

Share this post


Link to post
Share on other sites

Actually Scene_Map clears the scene stack when it starts so it's fine to goto right to it. Probably mostly because items that run common events need to goto the map scene before they will work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
Top ArrowTop Arrow Highlighted