Summary:
I just did some "homework" about how to call common events via script
I just did some "homework" about how to call common events via script
- $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.
- An example of how to call common event #3 immediately :
JavaScript:this.seupChild($dataCommonEvents[3].list, this._eventId); this.updateChild(); - 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(); - You may want to wrap that in a function.
You should call it like this: this.callCommonEvent(3)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(); }
If you want to run it immediately, then call it like this: this.callCommonEvent(3, true)
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:
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:
If you are sure the event is not running during map transfer, you can make it simpler:
If you want to run it right now, you just need to call the updateChild method:
And since common event is a list of commands, it is easy to extend them:
Or maybe:
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 );