MZ Script call to plugin command

Is there a way to call a plugin command from a script? I was following a tutorial on plugin scripting and wanted to call a window from the main menu (using for example visustela window core) but since it requires js input I'm puzzled on what I should write to call the plugin command.


I've got the following written for a plugin command that calls a window scene I've prepared:

PluginManager.registerCommand('factions', 'open', args => {
SceneManager.push(MyScene);
})
The method you're seeking is PluginManager.callCommand. An interpreter reference is expected, so you should pass a reference to an appropriate interpreter instance, e.g.
JavaScript:
var interpreter = $gameMap._interpreter;
var pluginName = 'factions';
var commandName = 'open';
var args = {};
PluginManager.callCommand(interpreter, pluginName, commandName, args);
If you're in battle, consider $gameTroop._interpreter instead. However, this shouldn't make a difference unless your plugin command is context-dependent.

Invoking plugin commands remotely is much simpler if you write the commands as public functions: you can then just call those functions directly, e.g.
Spoiler: Example plugin code
JavaScript:
// example.js //
/*:
 * @target MZ
 * @plugindesc example plugin
 * @help help text
 *
 * @command test
 * @desc Say hello!
 */

window.myStuff ||= {};  // author namespace (global)
($ => {                 // $ = myStuff.myPlugin = plugin namespace
  const PLUGIN_NAME = 'example';
  $.sayHello = function() { alert('Hello!'); };
  PluginManager.registerCommand(PLUGIN_NAME, 'test', $.sayHello);
})(myStuff.myPlugin ||= {});
For this plugin, the following script call is identical to the test command:
JavaScript:
myStuff.myPlugin.sayHello();
This works because myStuff is defined on the global object, window, so all of its properties, including the sayHello function, are also globally accessible.

(I've structured the example like this partly for organisational purposes, and partly to avoid defining any unnecessary global variables.)
Thanks for the quick response! If I understood the first part correctly, I should use the callCommand function in a script to call the existing plugin command while declaring each variable beforehand? Then about the public functions, the line "window.myStuff ||= {}; " in particular the "||= {}" syntax is the one that defines it as public? I'm new to js, so bear with me haha.
@pf111 - yep, you should be able to put this entire thing in a Script field (and change the values to fit the plugin name / command name):
JavaScript:
var interpreter = $gameMap._interpreter;
var pluginName = 'factions';
var commandName = 'open';
var args = {};
PluginManager.callCommand(interpreter, pluginName, commandName, args);
You could just put the values in directly, e.g.
JavaScript:
PluginManager.callCommand($gameMap._interpreter, 'factions', 'open', {});
I just figured it would be easier to keep track of what's going on by writing it out with named variables.


In JavaScript, the global object for foreground tasks (e.g. a game!) is window. "Global" just means "accessible regardless of scope". If you define a new property on window, that property will also be globally-accessible. More details here:
||= is the "logical or assignment" operator; {} is shorthand for an object with no original properties. I used ||= in my example to initialise the value only if it doesn't already exist (all objects are truthy in JavaScript). More details here:
I also used an immediately-invoked function expression (a.k.a. IIFE). This creates a "private" scope for the PLUGIN_NAME constant, for example. More details here:
Hopefully that helps to clarify things a bit~ 😮‍💨
Just got to try this (had several work heavy days) and it worked! Thanks again!

Original thread: https://forums.rpgmakerweb.com/threads/script-call-to-plugin-command.139593/