MZ Why isn't my non-arrow function plugin command called?

Following https://forums.rpgmakerweb.com/index.php?threads/plugin-commands-using-event-id.141662/post-1225017
Where Casper667 suggests de-anonymizing removing arrow functions from your plugin command so that keyword this will function better,

I wrote my registration like this:

PluginManager.registerCommand(pluginName, "TurnInAQuest", Game_Event.prototype.TurnInAQuest);

But my function

Game_Event.prototype.TurnInAQuest = function()
{
SoundManager.playBuzzer();
$gameMap.event(this._eventId).getQuestHub().TryTurnInQuest();
}

is not called (i.e. the buzzer sound doesn't play.)
Just to note, that's an issue with arrow functions not having their own this value. It will work perfectly OK with a standard function, anonymous or otherwise. E.g.
JavaScript:
PluginManager.registerCommand("MyPlugin", "CommandName", function() {
  alert("This command was called from event ID " + this.eventId() + ".");
});

Ideas:
  1. If a plugin runs into an error while it is loading, the engine will simply skip that plugin completely. Check the Console tab of the dev tools window (F8 or F12 during playtest) for any error messages.

  2. Does the value of the pluginName variable match the name of your plugin file, without extension? E.g. if the file is named MyPlugin.js, pluginName should have the value "MyPlugin".

  3. Have you defined the corresponding plugin command correctly in the header? E.g.
    JavaScript:
    /*:
    ...
     * @command TurnInAQuest
     * @text Turn in a Quest
     * @desc Turns in a quest!
     */
Also, like Casper mentions in that thread you linked, note that plugin commands are run in the context of the calling interpreter. It doesn't matter what object the function is defined on: the command invokes an instance of the provided function that is bound to a specific context. So it doesn't make much sense to have the command bound to a method of a different class (Game_Event).

I've moved this thread to Learning JavaScript (from MZ Support). Thank you.

So I removed the Game_Event.prototype part (and yes I did it in both places). So now the function is just TurnInAQuest.

  • If I declare it before registering it (that is, I define it on line 590 outside of the "dark wizard" scope ( function () {})(); and register on line 610), I get the error "Cannot read property 'event' of null" as soon as my game tries to load. Obviously it is trying to read the current game map when that isn't defined yet.
  • However, if I define it after line 610, that is after I register it, I get "TurnInAQuest" is not defined.
  • It only seems to work if I declare it with function() {...} in the registration command.
Based on this, it seems that the syntax given in the other thread is abbreviated and that there is more I needed to put there in order to get this working. Moreover by attaching my function to an existing object I actually prevented some errors from being thrown, even though the underlying code was still bad.

Edit: It works if it is declared like this,

PluginManager.registerCommand(pluginName, "TurnInAQuest", function() {
$gameMap.event(this._eventId).getQuestHub().TryTurnInQuest();
});

and since I have access to this in such a function, it will serve my purposes perfectly.
Wouldn't it also work if you ignored trying to use this and simply passed in the object you want to reference as an argument to the function?

Code:
PluginManager.registerCommand(pluginName, "TurnInAQuest", args => { TurnInAQuest(this) });

TurnInAQuest = function(event)
{
   SoundManager.playBuzzer();
   $gameMap.event(event._eventId).getQuestHub().TryTurnInQuest();
};
since I have access to this in such a function, it will serve my purposes perfectly.
Great! 😮‍💨
Spoiler: extra notes
If I declare it before registering it (that is, I define it on line 590 outside of the "dark wizard" scope ( function () {})(); and register on line 610), I get the error "Cannot read property 'event' of null" as soon as my game tries to load.
Functions do not evaluate their contents unless you tell them to do so. Maybe it was an errant IIFE. Just to remind you: we can't see your code. I don't get any useful information from comments like 'on line 590 outside of the "dark wizard" scope'. (Maybe my wizardry isn't dark enough.)

Like I said, registerCommand will bind the given function to the calling Game_Interpreter, regardless of the function's usual this value. In case it helps, this is the kind of structure I generally use:
JavaScript:
(() => { // some iife
  // Ensure global namespace.
  globalThis.DarkWizard ??= {};
// ...
  // Define plugin command function on that namespace.
  DarkWizard.somePluginCommand = function(args) {
    SoundManager.playBuzzer();
    this.character(0).getQuestHub().TryTurnInQuest();
  };
  // Register the plugin command.
  PluginManager.registerCommand("MyPlugin", "TurnInAQuest", DarkWizard.somePluginCommand);
})();
(this.character(0) is just a Game_Interpreter shorthand for "This Event".)

The namespace structure helps to avoid defining lots of global variables. Defining a globally-accessible function for each plugin command makes it easily available outside of the Plugin Command structure, e.g. for:
  • Invoking the plugin command's functionality via script call, or
  • Creating plugin patches for extension, compatibility, and/or bug fixes.
It's less helpful in this kind of situation, though: your command relies on an interpreter instance, which generally means you'll have access to plugin commands the usual way.

Wouldn't it also work if you ignored trying to use this and simply passed in the object you want to reference as an argument to the function?

Code:
PluginManager.registerCommand(pluginName, "TurnInAQuest", args => { TurnInAQuest(this) });
No. That would register the command to a function using the context at the time of registration, because objects (this in this case) are passed by reference.

As an example of this, check out Game_Interpreter#setupChoices in rmmz_objects.js: it sets a callback function on $gameMessage that in turn sets values on this interpreter.
One last thing to mention (apologies if this is obvious but I think I should mention it for anyone else new to writing plugins): For this kind of plugin registration, you need to use

function(args)

It won't work if you write

function(a, b, c)

All of the args will be dumped into the first variable listed. So in the latter case, upon inspection,

a = {"a": "value1", "b": "value2", "c": "value3"} // this is javaScript args object
b = undefined
c = undefined

So you instead get the values of the arguments by

function(args) {
let a = args["a"];
let b = args["b"];
let c = args["c"];
...
}

also note the values are PASSED AS STRINGS EVEN IF YOU USE @Number. So you for instance if b is a number, you have to write:

let b = Number(args["b"]);

That is all, thanks for your help everyone 🙂
I believe you can do functon (a,b,c) as args if you want multiple.
you can try function(...args) as it takes multiple ones (arrays, objects alike I believe).
I believe you can do functon (a,b,c) as args if you want multiple.
you can try function(...args) as it takes multiple ones (arrays, objects alike I believe).
No; like the OP said, MZ plugin command functions receive a single object argument.

What you can do is use destructuring syntax for that argument, e.g.
JavaScript:
function f({ a, b = 1, ...c }) {
  console.log("a is", a);
  console.log("b is", b);
  console.log("c is", c);
}
f({ a: 5, cake: "tasty", d: 123 });
// > a is 5
// > b is 1
// > c is Object { cake: "tasty", d: 123 }
However, you will still have to parse any arguments from string as necessary.

You can find further details/explanation here~

Original thread: https://forums.rpgmakerweb.com/threads/why-isnt-my-non-arrow-function-plugin-command-called.166981/