roninator2 270 Posted December 30, 2021 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
Kayzee 4,060 Posted December 31, 2021 (edited) 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 December 31, 2021 by Kayzee Share this post Link to post Share on other sites
roninator2 270 Posted January 1, 2022 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
Kayzee 4,060 Posted January 3, 2022 (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 January 3, 2022 by Kayzee Share this post Link to post Share on other sites
roninator2 270 Posted January 3, 2022 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
Kayzee 4,060 Posted January 4, 2022 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
roninator2 270 Posted January 4, 2022 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
Kayzee 4,060 Posted January 6, 2022 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
kyonides 3 Posted January 23 I know the thread isn't recent at all, but I wanna add some information here for our fellow member @roninator2. In many Object Oriented Programming (OOP) languages, encapsulation is important. What it means is that it should NOT allow any object to easily get private information from another object. So internal objects of $game_map like @map_id should not be accessible for no good reason. That's why you create a method as an interface like setup(map_id) instead of just calling a setter method (a writer method in Ruby). That method will handle all the desired changes like changing the map's tileset and pretty much everything else. The best way to open access to any object is by creating a getter like attr_reader or setter like attr_writer or both like in attr_accessor :has_both_options I could even talk about Ruby internals, but that's a conversation for another day. Share this post Link to post Share on other sites