MV Call common events via script

Summary:
I just did some "homework" about how to call common events via script

  1. $gameTemp.reserveCommonEvent is not designed for this, it only runs the common event after current script is over and will override previously reserved event if you use it multiple times.
  2. An example of how to call common event #3 immediately :
    JavaScript:
    this.seupChild($dataCommonEvents[3].list, this._eventId);
    this.updateChild();
  3. An imporoved version, also handles multiple calls
    Code:
    var child = this;while(child._childInterpreter)  child = child._childInterpreter;
    child.setupChild($dataCommonEvents[3].list, child._eventId );
    child.updateChild();
  4. You may want to wrap that in a function.
    JavaScript:
    Game_Interpreter.prototype.callCommonEvent=function(ceid, immediately){  var child = this;
      while(child._childInterpreter)  child = child._childInterpreter;
      child.setupChild($dataCommonEvents[ceid].list, child._eventId );
      if(immediately) child.updateChild();
    }
    You should call it like this: this.callCommonEvent(3)
    If you want to run it immediately, then call it like this: this.callCommonEvent(3, true)


Spoiler: screenshots
1720015185124.png

1720015221929.png

ezgif-4-1e2473b5e4.gif



Spoiler: Long and boring study notes
It seems, by calling $gameTemp.reserveCommonEvent multiple times in a row, only the last one is actually execute.
Besides, the common event is reserved, means it is only called after current script has finished.
So if you want to call multiple common events via script, or execute them right here right now, what do you do?

It seems, the game runs scripts using a Game_Interpreter object. So in order to do that, you should find current interpreter, right?

Fortunately there's a run common event command so we can investigate. In rpg_objects.js, I found this:

JavaScript:
// Common Event
Game_Interpreter.prototype.command117 = function() {
    var commonEvent = $dataCommonEvents[this._params[0]];
    if (commonEvent) {
        var eventId = this.isOnCurrentMap() ? this._eventId : 0;
        this.setupChild(commonEvent.list, eventId);
    }
    return true;
};

Game_Interpreter.prototype.setupChild = function(list, eventId) {
    this._childInterpreter = new Game_Interpreter(this._depth + 1);
    this._childInterpreter.setup(list, eventId);

};

So this command117 calls a common event, using setupChild method. We just need a fraction of the code to run a common event.
For example, if you want to run common event #3, you only need to do this:

JavaScript:
this.setupChild($dataCommonEvents[3].list, this.isOnCurrentMap() ? this._eventId : 0);

If you are sure the event is not running during map transfer, you can make it simpler:

JavaScript:
this.seupChild($dataCommonEvents[3].list, this._eventId );

If you want to run it right now, you just need to call the updateChild method:

JavaScript:
this.setupChild($dataCommonEvents[3].list, this._eventId );
      this.updateChild();

And since common event is a list of commands, it is easy to extend them:

JavaScript:
if(this._childInterpreter && _childInterpreter._list){
  this._childInterpreter._list = [].concat(this._childInterpreter._list, $dataCommonEvents[3].list)
}else{
  this.setupChild($dataCommonEvents[3].list, this._eventId );
}

Or maybe:

JavaScript:
var child = this;
while(child._childInterpreter)  child = child._childInterpreter;
child.setupChild($dataCommonEvents[3].list, child._eventId );
You might want to start your post with some summary of what it's about, especially since posts in this sections are usually asking for help.

And since common event is a list of commands, it is easy to extend them:
A neat thing about that: you don't even need an event to use setupChild, once you learn the format you can use that function to execute any dynamically created list of event commands you want. This is useful e.g. when you want to show a message from a script, but it doesn't fit inside a single message box.
I made a code to execute all common events in a row 1 by 1 in queue.
there are 2 others, DK had one and HIME I believe, so you can use
any of them to play the common event in a row that way.

depending what the common event does and how they are executed.
@Mac15001900 Yeah, I just did that. It looks like some random notes before.
@ShadowDragon Oh, I didn't know. Anyway, I just edit the post. I decided to use child interpreters instead of a long list.

Original thread: https://forums.rpgmakerweb.com/threads/call-common-events-via-script.169991/