MZ Plugin Command works, but can I access my plugin via Script in Event Editor?

I have a very simple plugin that uses 1 parameter.
When the player triggers an event with the Action Button, the plugin will update a variable inside my plugin and store the eventID. When the player presses and hold the "ok" button, I have a popup window at the top that shows values stored.

This works, however, I don't want to type in the eventId parameter via Plugin Command in the Event Editor every time when I set this up. You'd think I could do a this._eventId and pass that into my plugin somehow.
I get the impression that I could do that with Script in the Event Editor. I have no confirmation if this is doable or not. Any attempts I made at using script so far do not work. So either it doesn't work, or there is a syntax/setup I do not know.

Notes:
Yes, I know the event editor is set up to give me infinite 0 gold.

End goal is to have multiple treasure chests that respawn their contents after specified number of seconds, each with its own instance of a timer. This way I could have an area of respawning chests. When using the plugin, I envision that I could write a Script in the Event Editor to streamline this a little bit so I am only inputting the duration of the timer, not both the duration of the timer AND the eventID because the eventId appears in the top header of the Event Editor and seems like it could be accessible.
If this works, I would copy and paste the chest event with a X timer set 100 times, without changing the eventID manually. And still have the flexibility to change the timer duration for specific chest if so desired. This would be a huge timer saver when it comes to setting up the events.
Basically, I want to ability to use both the Plugin Command or a Script in Event Editor to access my Plugin. I've been having difficulty finding something that makes this clear whether or not Script will work in this way.

Plugin Code
//============================================================================= // PluginWithParametersTest // PluginWithParametersTest.js //============================================================================= /*: * @target MZ * @plugindesc Testing plugin Command with parameters * @author Me * @help Testing plugin Command with parameters * @url N/A * * @version 1.0 * @website N/A * @license pending * * @param eventId * @text Event ID * @desc The ID of the event affected. * @type number * @default 1 * * @command set * @text Set eventId * @desc Set eventId * * @arg eventId * @text Event ID * @desc The ID of the event affected. * @type number * @default 1 * */ var eventId = null; // debugging popup window var popUpWindow = null; (function() { var pluginInfo = { name: "PluginWithParametersTest", description: "PluginWithParametersTest", version: "1.0" }; // Register your command(s) PluginManager.registerCommand(pluginInfo.name ,"set", function(args) { //put your code here for your plugin command. eventId = Number(args.eventId); $gameMessage.add("eventId: " + eventId + " as plugin command."); }); // Add the update function to the game loop. var _Scene_Map_update = Scene_Map.prototype.update; Scene_Map.prototype.update = function() { _Scene_Map_update.call(this); update.call(this); }; // allows a fake debug dialog appear when triggered by "ok" var update = function() { if (Input.isPressed("ok")) { if (!popUpWindow) { popUpWindow = new Window_Base(new Rectangle(0, 0, Graphics.boxWidth, 100)); popUpWindow.drawText("Plugin With Parameters Test", 0, 0, Graphics.boxWidth, "center"); popUpWindow.drawText(eventId, 0, 28, Graphics.boxWidth, "center"); SceneManager._scene.addChild(popUpWindow); } } else { // Remove the pop-up window when the key is released. if (popUpWindow) { SceneManager._scene.removeChild(popUpWindow); popUpWindow = null; } } }; })();
Event Editor Contents
◆Set Movement Route:This Event (Wait) : :◇Direction Fix OFF : :◇Turn Left : :◇Wait:3 frames : :◇Turn Right : :◇Wait:3 frames ◆Change Gold:+ 0 ◆Text:None, None, Window, Bottom : :0\G were found! ◆Plugin Command:PluginWithParametersTest, Set eventId : :Event ID = 42
Event Editor Screen Shot
rpgmakermz_eventeditorscreenshot_plunginghelp_01.jpg

I imagine Script would look like this but it never accesses my code:

◆Set Movement Route:This Event (Wait) : :◇Direction Fix OFF : :◇Turn Left : :◇Wait:3 frames : :◇Turn Right : :◇Wait:3 frames ◆Change Gold:+ 0 ◆Text:None, None, Window, Bottom : :0\G were found! ◆Script:var eventId = this._eventId; : :$gameMap._interpreter.pluginCommand("PluginWithParametersTest", "set", { eventId: eventId});

Screenshot of Event Editor Using Script instead of Plugin Command:
rpgmakermz_eventeditorscreenshot_plunginghelp_02.jpg
Welcome!

Without intending to sound patronizing, did you write this? There's a strange mixture of correctly-written code and bad practice and erroneous syntax.

This:
// Register your command(s)
PluginManager.registerCommand(pluginInfo.name ,"set", function(args) {
//put your code here for your plugin command.
eventId = Number(args.eventId);
$gameMessage.add("eventId: " + eventId + " as plugin command.");
});
is rather weird. The normal process would be to write a regular function lower in your plugin, and what you put here for the plugin command simply parses the argument and calls that function.

If you did it that way, it would solve your entire problem because you'd just call that function directly. Also, you shouldn't just be using "eventId" without declaring it, because that creates it as a globally-scoped variable which is unnecessary and undesirable.

So, for example,
Code:
PluginManager.registerCommand(pluginInf.name, "set", function(args) {
    let eventId = Number(args.eventId);
    PluginInfo(eventId);
});

PluginInfo = function(eventId)
{
    $gameMessage.add("eventId: " + eventId + " as plugin command.");
};

Then in your event you can do a Script of PluginInfo(this.eventId())

Note me using the getter function eventId(), rather than directly accessing the protected property. That's something you do quite a bit in your code, and it's considered bad practice because it may cause incompatibilities/conflicts with anything that is supposed to happen inside of those getters.

Code:
        _Scene_Map_update.call(this);
        update.call(this);
    };

    var update = function() {
This is also weird. Why is this separate from the function above? And why declare update as a function expression? It's globally scoped anyway (with a very ubiquitous name, which is not great practice), so I don't see the point. Put all of that code into your overwritten Scene_Map.update() method.

Anyway, good luck with your project.
Welcome!

Without intending to sound patronizing, did you write this? There's a strange mixture of correctly-written code and bad practice and erroneous syntax.

This:

is rather weird. The normal process would be to write a regular function lower in your plugin, and what you put here for the plugin command simply parses the argument and calls that function.

If you did it that way, it would solve your entire problem because you'd just call that function directly. Also, you shouldn't just be using "eventId" without declaring it, because that creates it as a globally-scoped variable which is unnecessary and undesirable.

So, for example,
Code:
PluginManager.registerCommand(pluginInf.name, "set", function(args) {
    let eventId = Number(args.eventId);
    PluginInfo(eventId);
});

PluginInfo = function(eventId)
{
    $gameMessage.add("eventId: " + eventId + " as plugin command.");
};

Then in your event you can do a Script of PluginInfo(this.eventId())

Note me using the getter function eventId(), rather than directly accessing the protected property. That's something you do quite a bit in your code, and it's considered bad practice because it may cause incompatibilities/conflicts with anything that is supposed to happen inside of those getters.


This is also weird. Why is this separate from the function above? And why declare update as a function expression? It's globally scoped anyway (with a very ubiquitous name, which is not great practice), so I don't see the point. Put all of that code into your overwritten Scene_Map.update() method.

Anyway, good luck with your project.

It is a strange mixture of me and AI coding because JavaScript is not my primary language.
I'm not going to lie, I don't like JavaScript and I was just trying to get something to work.

I cut it down to only this and and get a "pluginInfo is not defined" error when interacting with the event.

It never reaches the function. I'm at a loss. This was actually one first things I tried to do.

The code was saved. I have deleted the plugin and readded it to the plugin manager.
It is enabled. Have rebooted RPG Maker. Not starting from a save point either.
The other chest using plugin command still works. And there are no other plugins enabled and it's a fresh project.

(function() { // Register your command(s) PluginManager.registerCommand("PluginWithParametersTest", "set", function(args) { // Put your code here for your plugin command. let eventId = Number(args.eventId); // Call the pluginInfo function pluginInfo(eventId); }); // Define the pluginInfo function first let pluginInfo = function(eventId) { $gameMessage.add("eventId: " + eventId + " as plugin command."); }; })();

event editor is this:
◆Set Movement Route:This Event (Wait)
: :◇Direction Fix OFF
: :◇Turn Left
: :◇Wait:3 frames
: :◇Turn Right
: :◇Wait:3 frames
◆Change Gold:+ 0
◆Text:None, None, Window, Bottom
: :0\G were found!
◆Script:pluginInfo(this.eventId());
It is a strange mixture of me and AI coding because JavaScript is not my primary language.
I'm not going to lie, I don't like JavaScript and I was just trying to get something to work.

I cut it down to only this and and get a "pluginInfo is not defined" error when interacting with the event.

It never reaches the function. I'm at a loss. This was actually one first things I tried to do.

The code was saved. I have deleted the plugin and readded it to the plugin manager.
It is enabled. Have rebooted RPG Maker. Not starting from a save point either.
The other chest using plugin command still works. And there are no other plugins enabled and it's a fresh project.

(function() { // Register your command(s) PluginManager.registerCommand("PluginWithParametersTest", "set", function(args) { // Put your code here for your plugin command. let eventId = Number(args.eventId); // Call the pluginInfo function pluginInfo(eventId); }); // Define the pluginInfo function first let pluginInfo = function(eventId) { $gameMessage.add("eventId: " + eventId + " as plugin command."); }; })();

event editor is this:
◆Set Movement Route:This Event (Wait)
: :◇Direction Fix OFF
: :◇Turn Left
: :◇Wait:3 frames
: :◇Turn Right
: :◇Wait:3 frames
◆Change Gold:+ 0
◆Text:None, None, Window, Bottom
: :0\G were found!
◆Script:pluginInfo(this.eventId());
You have your code wrapped in an IIFE, so that function is not available globally. The most common solutions would be to either move the pluginInfo function outside of the IIFE, or just remove the IIFE (since it's serving no purpose in this particular snippet anyway).
I disagree, the IIFE is useful here as-is: it keeps those var statements, e.g. _Scene_Map_update, out of the global scope. I recommend using let or const instead of var, because its scope rules can be confusing. Generally things are {block} scoped, more details here:

You can manually define properties on the global object, e.g. globalThis, regardless of the scope in which you are making that definition.

A better solution would be to declare a single global variable and use that as a namespace for all your code, e.g.
JavaScript:
globalThis.Rinji ??= {};
Rinji._Scene_Map_update = Scene_Map.prototype.update;
// etc
That avoids polluting the global namespace while keeping all your code globally-accessible.
I disagree, the IIFE is useful here: it keeps those var statements, e.g. _Scene_Map_update, out of the global scope.
I had specified that my statement only pertained to the particular snippet that I quoted (where the OP had removed those var statements), but I suppose I probably should have clarified that things could change if those types of statements were added back in.
Oh I see, apologies. 😴
It is a strange mixture of me and AI coding
That's what I figured - most of the coders here have stated they're unwilling to troubleshoot/fix AI-generated code.

I offered the above explanations extending the benefit of the doubt that you were still learning or something, but if you're not proficient enough to understand the conversation I suggest you backtrack and just post a request for help implementing what you want to achieve.
I had specified that my statement only pertained to the particular snippet that I quoted (where the OP had removed those var statements), but I suppose I probably should have clarified that things could change if those types of statements were added back in.

I disagree, the IIFE is useful here as-is: it keeps those var statements, e.g. _Scene_Map_update, out of the global scope. I recommend using let or const instead of var, because its scope rules can be confusing. Generally things are {block} scoped, more details here:

You can manually define properties on the global object, e.g. globalThis, regardless of the scope in which you are making that definition.

A better solution would be to declare a single global variable and use that as a namespace for all your code, e.g.
JavaScript:
globalThis.Rinji ??= {};
Rinji._Scene_Map_update = Scene_Map.prototype.update;
// etc
That avoids polluting the global namespace while keeping all your code globally-accessible.
Thanks, I’ll try this tonight. That sounds like the way it should be approached.

The _Scene_Map_update was an ugly work around because console.log(); didn't seem to be working at one point. I’d rather not have to do that when something already exists.

When I saw the Using Functions to Enclose the Entire plugin Section in the Plugin Tutorial I was under the impression the IIFE was required to make a plugin. I tried looking at few plugins and they all seemed to use this structure as well. The next section of the tutorial showed some register command code without IIFE, and I guess my brain jumped to the assumption that this code was always supposed to go in the IIFE.

If there was a namespace in the other plugins I looked at, I either missed it or that particular plugin didn’t use one.

Yep, the example structure given in that tutorial does not directly expose any global variables.

For some reason I didn't think of this earlier, but it is possible to invoke plugin commands manually, via a Script command. E.g.
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
});
However, it can make things easier (and marginally more efficient) if there's a way to call the function without going through the PluginManager.

Personally, for my "full" MV/MZ plugins I use an author namespace (CAE) with 1 sub-namespace per plugin (e.g. ChoiceShuffle). However, I often don't bother with that for quick/small plugins, especially if they don't have any parameters.
m

Original thread: https://forums.rpgmakerweb.com/threads/plugin-command-works-but-can-i-access-my-plugin-via-script-in-event-editor.162045/