How can I extend a class like Game_Event?

So I started adding a few functions inside Game_Event's class declaration but is there any other way of adding function/variables without touching the original one? O saw some scripts where they just on their own code doing:

Code:
class Game_Event   #declares their stuffend
  Is it the best way of doing? Oh and also when I'm inside event's page what's the scope of the script calls from there? Why can't I just call the Game_Event's functions directly instead of doing $game_map.events[@event_id].functionName everytime?
It really doesn't matter whether you edit the original class and add your extra methods and variables, or if you make a new script for the class and add them there. When the scripts are read, the original class is read, and when your additions are found in a later script, they're just added to the class anyway, so you end up with the same result.

People use additional script slots instead of adding their changes to the original script for several reasons

- so their script can be easily shared

- so their script can be easily copied from one project to another

- so the original class is not messed up while they're developing their script

- because they like to keep the RTP stuff and their edits separate

- probably half a dozen other reasons that I haven't thought of

I prefer to have them all together, so I make my changes in the original class. That way, when I'm looking for something to do with Game_Event, I don't have to look through a dozen different scripts to find what I'm after. I also don't like using lots of aliases and having the engine jump through all the different definitions (I don't really know if it increases processing time by a miniscule amount or not, but my aim is to avoid unnecessary processing wherever possible) or overwriting classes.

The Call Script command is executed from the Game_Interpreter class, not from Game_Event, so if you just call functionName, Game_Interpreter has no idea what object you're talking about. That's why you have to tell it to call the functionName method of a Game_Event ($game_map.events[@event_id]). Another way to do it without having to do all of that is to put a Set Move Route on the current event and use the Script call within the move route. THAT is executed from the Game_Event class, so you CAN call functionName there without specifying the object, and the event will know it's running the script on itself.

You can add this method to Game_Interpreter:

Code:
def event  $game_map.events[@event_id]end
then anytime you want to use a Call Script on $game_map.events[@event_id], just say event.functionName instead.
Those are some very awesome tips Shaz 🙂 Didn't know about Set Move Route scope! That's awesome!
That's only useful, of course, if you don't already have a move route running on the event, otherwise it'd cancel out (stop) a previous move route that was still executing. You could put it as a command within an existing move route rather than on its own.


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.


Also moving to RGSSx Script Support, since you weren't actually requesting a script

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-extend-a-class-like-game_event.17481/