MZ Javascript code to run plugin command?

I apologize if this is already covered somewhere, but I've googled and binged my way across half the internet and can't find the answer...and least, not one that I understand....

I have a couple of plugins that do exactly what I want, but plugin commands are only available via events (common or otherwise), and I would very very much like to use their functionality through scripting.

Is there a standard way in MZ, using javascript, to run a plugin command?
no, because that would be doing it backwards.

the plugin commands are already a complex interface to allow events to call on javascript functions without programming knowledge. If you are already in javascript, you should call those functions directly instead of making a wrapper to call a wrapper that does nothing but call functions you can already call directly.

what you need to do is go to those parts of a plugin that handles plugin commands, check which internal function those plugins call after analysing the plugin commands, and then call those functions directly.
I don't know a "standard" way necessarily, but if you understand a little javascript, it's usually fairly straightforward to track down the functions and arguments that the plugin commands are actually calling, and just call those functions directly with script calls.

Easiest way is to do a "find" on the exact, case-sensitive plugin command. Look for where that command is taken as a string argument by a function that looks like it's handling plugin commands, e.g. in MZ I believe it would look like "PluginManager.registerCommand(x, y, z)"--the command you're looking for should be one of the x, y, or z string arguments being passed to that function. When you find the function that's taking that plugin command as an argument, you've most likely found the code that is run when you use that plugin command. You can then grab that code, manually setting any necessary arguments that would have been passed in the plugin command, and use it as a script call.
JavaScript:
PluginManager.callCommand(self, pluginName, commandName, args);
  • self is a Game_Interpreter reference;
  • pluginName is the name of the plugin (string) without the ".js" extension;
  • commandName is the name of the command (string);
  • args is an object in the form {paramName1: value, paramName2: value, ...}.
Example: ButtonPicture is a plugin present by default in new RMMZ projects. To call its plugin command, set, try this in a Script command:
JavaScript:
PluginManager.callCommand(this, 'ButtonPicture', 'set', {
  pictureId: 1,
  commonEventId: 5
});
1607106165362.png

That is a function from the rmmz_managers.js file. it shows how to trigger a plugin command.

you should be able to then do something like this as a script call:
Code:
const event = this;
const plug_name = "AwesomePlugin";
const comm_name = "BestCommandEv";
const comm_args = "somearg1 somearg2";
PluginManager.callCommand(event, plug_name, comm_name, comm_args);

You MUST actually pass the correct event if you are using some command that would manipulate the event in some way. (if running the script from the desired event, using 'this' as shown above is suitable).

You should replace plug and comm name strings with the relevant strings for your commands. Also, I've never used a plugin command myself, so I'm not entirely sure of the format that 'comm_args' would be in. its either going to be a string with each 'argument' seperated by a space, or an array of strings:
Code:
const comm_args = "somearg1 somearg2"; // spaced string
const comm_args = ["somearg1", "somearg2"];// array


Anyway, hope that helps! :*

edit: damn ninjas. I knew I took too long 😀
Oh, seems the comm_args is supposed to be an object as shown by @caethyril ! Nice! Learned something new 😀
no, because that would be doing it backwards.
.......
what you need to do is go to those parts of a plugin that handles plugin commands, check which internal function those plugins call after analysing the plugin commands, and then call those functions directly.
I would love to do that - unfortunately, they're pretty well obfuscated.
The parameters and whatnot are there for me to look at (per the other instruction),
but the actual code....not so much.
...unfortunately, they're pretty well obfuscated...
Just in case your not aware - obfuscated code has many additional downsides. Performance impact being a main one. I'm not going to derail your thread, but I recommend looking into it further. 🙂
Just in case your not aware - obfuscated code has many additional downsides. Performance impact being a main one. I'm not going to derail your thread, but I recommend looking into it further. 🙂
I'll keep that in mind. Thanks.
I hope the Mod Team doesn't mind me clarifying this for anyone else who comes here looking for answers related to this topic... the following reply to the OP's question is a bit... well... inaccurate and rather misleading for anyone "new" to JavaScript...

no, because that would be doing it backwards.

the plugin commands are already a complex interface to allow events to call on javascript functions without programming knowledge. If you are already in javascript, you should call those functions directly instead of making a wrapper to call a wrapper that does nothing but call functions you can already call directly.

what you need to do is go to those parts of a plugin that handles plugin commands, check which internal function those plugins call after analysing the plugin commands, and then call those functions directly.

I don't think that's actually "doing it backwards" since there are cases where (like with ALL Visustella MZ Plugins) the code is forbidden by the OC devs to actually make human-readable without violating the TOS, so the only option is to simply call the Plugin Command directly in your own code (like in your own external Plugin code) to trigger the "action" you want to do via the method Global Mod Caethyril suggested in the quote below:

JavaScript:
PluginManager.callCommand(self, pluginName, commandName, args);
  • self is a Game_Interpreter reference;
  • pluginName is the name of the plugin (string) without the ".js" extension;
  • commandName is the name of the command (string);
  • args is an object in the form {paramName1: value, paramName2: value, ...}.
Example: ButtonPicture is a plugin present by default in new RMMZ projects. To call its plugin command, set, try this in a Script command:
JavaScript:
PluginManager.callCommand(this, 'ButtonPicture', 'set', {
  pictureId: 1,
  commonEventId: 5
});

Doing this method is pretty much the only option when you're faced with obfuscated plugins in RPG Maker MZ. Since you're most likely NOT permitted by the plugin's OC Dev to un-obfuscate it, you can only use the Plugin Commands and other "Globally Accessible" functions the dev made available to you, the end user.

Hope this clarifies this confusing thread's list of mixed yes/no answers as well as the WHY you would use the JavaScript method in certain cases.
I agree with Andar that, if possible, it's better to avoid going through an extra layer of abstraction (i.e. the PluginManager). Otherwise, yes, PluginManager.callCommand is the way to go.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/javascript-code-to-run-plugin-command.130626/