MV Simple script call plugin?

How do I do this without causing a maximum call stack?

Context:
I'm creating a UI for a minigame and I don't want to use a mess of script inputs because I'll need to call it multiple times. I understand I can call it from Common Events, but I'd like to just put it all in a plugin and update text values as I go.

I feel like it should be this simple, as it would be in any other engine:

var _Game_Interpreter_pluginCommand =
Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
_Game_Interpreter_pluginCommand.call(this, command, args);
if (command === 'MyVariable') {
MyVariable();
}
};

function MyVariable() {
$gameScreen.showPicture(1, 'SomeImage', 0, 8, 8, 100, 100, 255, 0);
// Other commands
}

But this causes a maximum call stack, I assume because when the plugin is called it calls it 1 billion times. Any way to pull this off?
What you have should be fine. Do you by chance have the plugin installed multiple times or the same code tucked away in another plugin elsewhere? You shouldn't be getting that error unless both the current version of the function and the aliased version are the same.
make sure its not on parallel or autorun event which stacking too.
if its on action trigger, than turn it off and see if it happens so it
isn't another plugin Robro33 said 🙂

this stack if its not yours can be your plugin order too.
Like Robro said, it's probably another plugin defining a global _Game_Interpreter_pluginCommand variable.

Two common, safer approaches here:
  1. Use an IIFE to ensure your plugin's code is privately scoped:
    JavaScript:
    (function() {  // IIFE for function scope.
      // This variable is now declared in the function scope, not the global scope.
      var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
      Game_Interpreter.prototype.pluginCommand = function(command, args) {
        // etc
      };
    })();
    This prevents a naming clash but means other plugins cannot access your code.

  2. Define a single global variable as a "namespace" for your plugin code, e.g.
    JavaScript:
    // Define your global namespace if it doesn't already exist.
    window.Khali || (window.Khali = {});
    // Declare all your otherwise "global" variables on that namespace.
    Khali._Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
      // etc
    };
    The namespace is visible to other plugins/code because it's global, but now there's only one, hopefully non-clashing, global variable containing as many values as you need.

    If you write multiple plugins, you may find it helpful to declare a sub-namespace for each plugin to keep things organised. E.g. if you want to patch the same method in 2 different plugins.
You could use either approach, or both together.
Like Robro said, it's probably another plugin defining a global _Game_Interpreter_pluginCommand variable.
100% this is what it is. I also didn't realize I could use namespaces this way in JS, but mainly because I never dove deep into JS.

Super useful replies, thanks!

Original thread: https://forums.rpgmakerweb.com/threads/simple-script-call-plugin.178970/