MZ How hard is it to make a basic plugin command?

Hi folks,

I have a lot of script calls I use in various common events which pass game variables into them which are set before running that common event, such as:

JavaScript:
$gameVariables.setSelfValue([$gameVariables.value(1), $gameVariables.value(2), $gameVariables.value(3)], $gameVariables.value(4));
(Variables 1-4 are set in the event, then this script is run as a common event)

So, I was wondering how hard it would be for someone with no JS knowledge to make a plugin that does nothing but add a plugin command which accomplishes the same thing, but takes parameters for each of those variables instead of referencing a game variable.

Something like:
A plugin command with these params:
Param1 = Number
Param2 = Number
Param3 = Number
Param4 = Number

Then the command runs:
JavaScript:
$gameVariables.setSelfValue([param1, param2, param3], param4);

The reason I want to do this is because I use plugin-specific script calls for things that I might need to change later. To keep things flexible, I'll put the script call in a common event, make it accept variables as it's values, and then set those variables outside the common event before running it. This way, if I need to replace that plugin or change the script call, I can change it in the common event without having to change every instance of that script call, as the values for every different case were set outside of it.
Using a plugin command cleans that up, and allows me to edit the plugin itself instead of a common event if I need to change the script call, and the use of the script call in events will look cleaner since it would be one plugin command instead of setting several variables and then running a common event.

Thanks!

For some context, the setSelfValue script in the example is from a self-variables plugin I use. It has its own commands already, and this is just an example.
Making a plugin command in MZ isn't that difficult. The actual functional code would look something like this:

JavaScript:
const me = 'YourPluginName';
PluginManager.registerCommand(me, 'CommandName', function(args) {
	$gameVariables.setSelfValue([args.mapId, args.eventId, args.selfSwitch], args.value);
});

Then you'd need all the comment syntax to tell the editor how to handle your new command. That syntax would need to define what mapID, eventId, selfSwitch, and value actually mean in the above code.
/*:
* @target MZ
* @plugindesc Adds a plugin command to call $gameVariables.setSelfValue with twelve numeric parameters.
* @Author Echowake
*
* @command SetSelfValue12
* @text Set Self Value (12 Params)
* @desc Calls $gameVariables.setSelfValue with eleven array values and one target value.
*
* @arg Param1
* @type number
* @text Value 1
*
* @arg Param2
* @type number
* @text Value 2
*
* @arg Param3
* @type number
* @text Value 3
*
* @arg Param4
* @type number
* @text Value 4
*
* @arg Param5
* @type number
* @text Value 5
*
* @arg Param6
* @type number
* @text Value 6
*
* @arg Param7
* @type number
* @text Value 7
*
* @arg Param8
* @type number
* @text Value 8
*
* @arg Param9
* @type number
* @text Value 9
*
* @arg Param10
* @type number
* @text Value 10
*
* @arg Param11
* @type number
* @text Value 11
*
* @arg Param12
* @type number
* @text Target Value
* @desc The value to pass as the second argument.
*/

(() => {
PluginManager.registerCommand("YourPluginName", "SetSelfValue12", args => {
const values = [];
for (let i = 1; i <= 11; i++) {
values.push(Number(args[`Param${i}`]));
}
const target = Number(args.Param12);

// Call your existing function
$gameVariables.setSelfValue(values, target);
});
})();

Here you go..i dont know what you variable or functions are but i expanded it to 12 so you not set on four. Hope this helps.

Making a plugin command in MZ isn't that difficult. The actual functional code would look something like this:

JavaScript:
const me = 'YourPluginName';
PluginManager.registerCommand(me, 'CommandName', function(args) {
    $gameVariables.setSelfValue([args.mapId, args.eventId, args.selfSwitch], args.value);
});

Then you'd need all the comment syntax to tell the editor how to handle your new command. That syntax would need to define what mapID, eventId, selfSwitch, and value actually mean in the above code.

/*:
* @target MZ
* @plugindesc Adds a plugin command to call $gameVariables.setSelfValue with twelve numeric parameters.
* @Author Echowake
*
* @command SetSelfValue12
* @text Set Self Value (12 Params)
* @desc Calls $gameVariables.setSelfValue with eleven array values and one target value.
*
* @arg Param1
* @type number
* @text Value 1
*
* @arg Param2
* @type number
* @text Value 2
*
* @arg Param3
* @type number
* @text Value 3
*
* @arg Param4
* @type number
* @text Value 4
*
* @arg Param5
* @type number
* @text Value 5
*
* @arg Param6
* @type number
* @text Value 6
*
* @arg Param7
* @type number
* @text Value 7
*
* @arg Param8
* @type number
* @text Value 8
*
* @arg Param9
* @type number
* @text Value 9
*
* @arg Param10
* @type number
* @text Value 10
*
* @arg Param11
* @type number
* @text Value 11
*
* @arg Param12
* @type number
* @text Target Value
* @desc The value to pass as the second argument.
*/

(() => {
PluginManager.registerCommand("YourPluginName", "SetSelfValue12", args => {
const values = [];
for (let i = 1; i <= 11; i++) {
values.push(Number(args[`Param${i}`]));
}
const target = Number(args.Param12);

// Call your existing function
$gameVariables.setSelfValue(values, target);
});
})();

Here you go..i dont know what you variable or functions are but i expanded it to 12 so you not set on four. Hope this helps.
Thanks for these replies!

This should be exactly what I need. This is definitely easy enough for me to handle, so this should work great.
/*:
* @target MZ
* @plugindesc Adds a plugin command to call $gameVariables.setSelfValue with twelve numeric parameters.
* @Author Echowake
*
* @command SetSelfValue12
* @text Set Self Value (12 Params)
* @desc Calls $gameVariables.setSelfValue with eleven array values and one target value.
*
* @arg Param1
* @type number
* @text Value 1
*
* @arg Param2
* @type number
* @text Value 2
*
* @arg Param3
* @type number
* @text Value 3
*
* @arg Param4
* @type number
* @text Value 4
*
* @arg Param5
* @type number
* @text Value 5
*
* @arg Param6
* @type number
* @text Value 6
*
* @arg Param7
* @type number
* @text Value 7
*
* @arg Param8
* @type number
* @text Value 8
*
* @arg Param9
* @type number
* @text Value 9
*
* @arg Param10
* @type number
* @text Value 10
*
* @arg Param11
* @type number
* @text Value 11
*
* @arg Param12
* @type number
* @text Target Value
* @desc The value to pass as the second argument.
*/

(() => {
PluginManager.registerCommand("YourPluginName", "SetSelfValue12", args => {
const values = [];
for (let i = 1; i <= 11; i++) {
values.push(Number(args[`Param${i}`]));
}
const target = Number(args.Param12);

// Call your existing function
$gameVariables.setSelfValue(values, target);
});
})();

Here you go..i dont know what you variable or functions are but i expanded it to 12 so you not set on four. Hope this helps.
One quick question for you - if I wanted to run a script call in a plugin like this, and wanted to use this.eventId() as a value in the script it runs when the plugin command is run from inside an event, is there a way to do that? (such as getting the ID of the event running the command)

I tried to add a command to my plugin for running this script call:
JavaScript:
Ritter.unspawnEvent(this.eventId(), true);

But, when running the command from the plugin, it doesn't seem to be working the way running the script in an event would work (to get its own ID) and crashes the game. I'm assuming the "this" is pointing to the plugin instead of the event running the command or something.

This is what I have for this script call:

JavaScript:
/*:
 * @target MZ
 * @plugindesc Adds commands to spawn and erase events.
 * @Author Chris Cote
 *
 * @command EventEraseSelf
 * @desc Erase the event running the command, and remove saved data for that event.
 *
*/
(() => {
PluginManager.registerCommand('SpawnEventCommands', 'EventEraseSelf', (args) => {
    Ritter.unspawnEvent(this.eventId(), true);
});
})();
When you run a script call directly inside an event, the game knows which event is running it. So this.eventId() works, because “this” is the event’s interpreter — basically the little engine that’s executing that event’s commands.
But when you put the same code inside a plugin command, the meaning of “this” changes. In your version, you used an arrow function. Arrow functions don’t create their own “this” — they just grab whatever “this” was outside. In this case, that’s the plugin file itself, not the event interpreter. So when you call this.eventId(), the game goes: “Wait, I don’t know what event you’re talking about,” and it crashes.
The fix is to use a normal function instead of an arrow function. Normal functions let RPG Maker bind “this” to the interpreter that’s actually running the event. That way, when you call this.eventId(), it correctly gives you the ID of the event that triggered the plugin command.


/*:
* @target MZ
* @plugindesc Adds commands to spawn and erase events.
* @Author Chris Cote
*
* @command EventEraseSelf
* @desc Erase the event running the command, and remove saved data for that event.
*/
(() => {
PluginManager.registerCommand("SpawnEventCommands", "EventEraseSelf", function(args) {
// Here, "this" is the Game_Interpreter running the event
const eventId = this.eventId();
Ritter.unspawnEvent(eventId, true);
});
})();

Use function expression (function(args) { ... }) instead of an arrow function.
Arrow functions don’t bind their own this, so they inherit from the outer scope (the plugin file), which is why you were getting the wrong context.A normal function gets its this bound to the interpreter when called by the engine.Inside that function, this.eventId() will correctly return the ID of the event that ran the plugin command.You can then pass that ID into your Ritter.unspawnEvent call.

If you ever need the map ID as well, you can grab it from this._mapId inside the interpreter. For example:
const mapId = this._mapId;

That way you can uniquely identify the event across maps if your system needs it.

Hope this fix your issue and i explained in a way you understand.
When you run a script call directly inside an event, the game knows which event is running it. So this.eventId() works, because “this” is the event’s interpreter — basically the little engine that’s executing that event’s commands.
But when you put the same code inside a plugin command, the meaning of “this” changes. In your version, you used an arrow function. Arrow functions don’t create their own “this” — they just grab whatever “this” was outside. In this case, that’s the plugin file itself, not the event interpreter. So when you call this.eventId(), the game goes: “Wait, I don’t know what event you’re talking about,” and it crashes.
The fix is to use a normal function instead of an arrow function. Normal functions let RPG Maker bind “this” to the interpreter that’s actually running the event. That way, when you call this.eventId(), it correctly gives you the ID of the event that triggered the plugin command.


/*:
* @target MZ
* @plugindesc Adds commands to spawn and erase events.
* @Author Chris Cote
*
* @command EventEraseSelf
* @desc Erase the event running the command, and remove saved data for that event.
*/
(() => {
PluginManager.registerCommand("SpawnEventCommands", "EventEraseSelf", function(args) {
// Here, "this" is the Game_Interpreter running the event
const eventId = this.eventId();
Ritter.unspawnEvent(eventId, true);
});
})();

Use function expression (function(args) { ... }) instead of an arrow function.
Arrow functions don’t bind their own this, so they inherit from the outer scope (the plugin file), which is why you were getting the wrong context.A normal function gets its this bound to the interpreter when called by the engine.Inside that function, this.eventId() will correctly return the ID of the event that ran the plugin command.You can then pass that ID into your Ritter.unspawnEvent call.

If you ever need the map ID as well, you can grab it from this._mapId inside the interpreter. For example:
const mapId = this._mapId;

That way you can uniquely identify the event across maps if your system needs it.

Hope this fix your issue and i explained in a way you understand.
Thanks, this helped a lot!

Original thread: https://forums.rpgmakerweb.com/threads/how-hard-is-it-to-make-a-basic-plugin-command.180769/