Hi folks! I'm looking for a quick easy way for a plugin command to get the event id of the event that called the command. What the best way to pass the event id into the registered command?
MZ Plugin Commands using event id
Learning JavascriptYou should be able to just use:
Plugin commands, unless bound to a different object, will have the calling event's game interpreter context so you can use anything that belongs to game interpreter in a plugin command by default.
JavaScript:
this._eventId;
Plugin commands, unless bound to a different object, will have the calling event's game interpreter context so you can use anything that belongs to game interpreter in a plugin command by default.
this._eventId is returning undefined. For context:
JavaScript:
PluginManager.registerCommand("Command", frameName, (args) => {
target = args.target;
console.log(this._eventId);
someFunction(frameName, target);
});
I think in this case it will get the Window(global) context.
If you want to get the event Id on a plugin command, you can add this as a plugin:
JavaScript:
{
window.currentEventId = 0
window.currentInterpreter = null
const Game_Interpreter_command357 = Game_Interpreter.prototype.command357
Game_Interpreter.prototype.command357 = function(params){
window.currentInterpreter = this
if(this._eventId > 0){
window.currentEventId = this._eventId
}
return Game_Interpreter_command357.call(this, params)
}
}
Change the "currentEventId" and "currentInterpreter" to whatever you want.
What I did is add to a global variable the event id and current interpreter(optionally).
So you can access these values regardless of the context.
[EDIT] In my plugin, I have added this to my plugin namespace, like:
Eli.PluginManager.currentEventId
I just did it that way so you can understand. But you could also add the event id to a variable if you want:
JavaScript:
$gameVariable.setValue(varId, this._eventId)
You're using an arrow function is why. Is there any reason you can't do:
and then in someFunction you would do:
JavaScript:
PluginManager.registerCommand("Command", frameName, someFunction);
and then in someFunction you would do:
JavaScript:
someFunction = function(args) {
target = args.target;
console.log(this._eventId);
}
Hello, to get the event Id from the plugin command register method, use: $gameMap._interpreter._eventId
Moved to: Learning Javascript
Thanks all! Solved. 🙂
Thread closed
Original thread: https://forums.rpgmakerweb.com/threads/plugin-commands-using-event-id.141662/