How can I refer to self when scripting? Right now I'm using $game_map.events to refer to events, but
it would be so much easier if I could use something like self without worrying if I have the right event id.
this self
● ARCHIVED · READ-ONLY
-
-
The answer is in your topic title : use "self" to point to the current instance.
For example in Game_Event, self represent an instance of Game_Event. -
Ok, but how do I access its members?
I have a code which uses '$game_map.events[18].x' to check it's x position, but if I changed to self.x I get
"undefined method 'x' for #<Game_Interpreter...", and if I try $self.x I get "undefined method 'x' for nil:nilClass" -
Be careful. I'm no ruby guru, but it seems to me that the self keyword just means "current context". This means that inside a method definition, self refers to current instance, but inside a class definition (and outside of a method) self refers to the class itself (which in turn is an instance of the Class class but that's another story).
Anyway, there just a few cases were you actually need to explicitly use the self keyword. Most of the times ruby will automatically use the correct context.Please post the exact script that's troubling you, if you feel like it.
EDIT: ninja'd, you already posted some code
Ok, it looks like you're trying to run your code inside an event. Inside the event, the "execution contest" is not the event itself, but the Game_Interpreter class that's interpreting event commands. If you use $self.x you're trying to get the x property from a global variable named $self (that clearly does not exists).
I'm not 100% sure about it, but I think you can use self.x if you run a script inside a "Set Move Route" targeted at the event of your choice. -
Since that code was part of an event, it is actually ran inside the Game_Interpreter class so in that case, self refers to Game_Interpreter and not the event object
remember that self returns the instance of whatever class the self call was made... and for the case of events, event command processing is actually done inside the Game_Interpreter class so you won't be able to use self to denote the actual event object -
self. refers to the current class objects.
If you are in class Scene_Battle, self will be the scene objects. If you are in Game_Event, self will represent the event.
What you are looking to do is essentially tell the game interpreter what 'self' is; however, as self is ALWAYS going to be the current class and cannot be changed, you will need to use a different codeword. For example... this_event...
Something like this, for example...
#===============================================================================class Game_Interpreter#=============================================================================== #----------------------------------------------------------------------------- # #----------------------------------------------------------------------------- def this_event return $game_map.events[@event_id] endend
Then you would use something like
this_event.method as the script call during the event command processing.
I assume this is what you mean, if not, I highly advise you provide additional information on what you are trying to achieve. -
Awesome Dekita, worked great.
Thanks all =) -
Ok, I made a small test. You can use "self" to refer to an event ONLY inside a "Set Move Route" event command. That's because Game_Interpeter delegates execution of the "Set move route" commands directly to the correct Game_Event instance.
-
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.