MZ calling interpreter command126 via script call

hey guys, I am using mog treasure popup in my project and i am having a little difficulty calling the gain item command from script call.
to explain i have an array of numbers that i get a random entry from and add that entry to the inventory, the reason i am not using $gameParty.gainItem($dataItems[loot], 1); is because it does not call the treasure popup like it would in a normal gain item call
here is my code:
JavaScript:
//gain random item and call the mog treasure popup
Array.prototype.random = function () {
     return this[Math.floor((Math.random()*this.length))];
}
const pool = ["75",
              "77",
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              "83"
             ];
let loot = pool.random(); //ID OF ITEM YOU WANT;
var incDec = 0; //0 FOR INCREASE, 1 FOR DECREASE;
var constVar = 0; //0 FOR CONSTANT, 1 FOR VARIABLE;
var value = 1; //CONSTANT VALUE OR VARIABLE ID;
if (loot != null) {
     $gameMap._interpreter._params = [loot, incDec, constVar, value];
     $gameMap._interpreter.command126();
     //$gameParty.gainItem($dataItems[loot], 1); //does not work with treasure popup
     $gameSwitches.setValue(24, true); //not in use right now
     $gameVariables.setValue(93, loot); //not in use right now
}
if anyone can tell me what i am doing wrong that would be awesome, thanks in advance
What is it currently doing that it shouldn't, or not doing that it should?
Any time you're asking for help, you need to make sure you tell us what's wrong. What is happening instead of what you want to happen?

Most people aren't going to want to read through a screenful of code you plop down when they don't know what they're looking for 😉

That said, I assume this isn't working because you're not actually passing anything into the command126 function. I'm not sure what you're going for on the line above, but command126 is expecting you to pass an array into it and you're not.
it should be adding an item to inventory instead it is giving me the error
Code:
MOG_TreasurePopup.js:187 Uncaught TypeError: Cannot read property '1' of undefined
    at Game_Interpreter.checkTreasurePopup (MOG_TreasurePopup.js:187)
    at Game_Interpreter.command126 (MOG_TreasurePopup.js:155)
    at Game_Interpreter.command126 (eval at <anonymous> (data: text/javascript;base64,dmFyIHRlbXBGdW5jPUZvc3NpbC5wb3N0TGlzdFswXS5wb3N0RnVuY3Rpb24udG9TdHJpbmcoKTt0ZW1wRnVuYz10ZW1wRnVuYy5zdWJzdHJpbmcodGVtcEZ1bmMuaW5kZXhPZigieyIpKzEsdGVtcEZ1bmMubGVuZ3RoLTEpO3RlbXBGdW5jKz0iIGlmKEZvc3NpbC5jaGF0dHlPdXRwdXQpe2NvbnNvbGUubG9nKEZvc3NpbC5wb3N0TGlzdFswXS5wbHVnaW5OYW1lKydQT1NURml4ZXMgSGVyZScpfSI7ICBldmFsLmNhbGwod2luZG93LHRlbXBGdW5jKTs=:1), <anonymous>:18:38)
Right, you're not passing any arguments into the command126 method, so when it tries to access members of the expected param array, it's undefined.
I have also tried $gameMap._interpreter.command126(loot, incDec, constVar, value); but it does not add the item but it does return true, is there something I am missing here?
I have also tried $gameMap._interpreter.command126(loot, incDec, constVar, value); but it does not add the item but it does return true, is there something I am missing here?
Yes. If a function is expecting multiple variables to be passed as arguments, its prototype declaration would look like you have above. However, command126 does not look like that. The declaration in rmmz_objects.js says: Game_Interpreter.prototype.command126 = function(params)
So it is only expecting one variable to be passed in. If you look on the following lines of code, you see references to params[0], params[1], params[2] and params[3], so that one variable is expected to be an array with 4 elements in it.

You seem like you kind of know this, because the line above in your code says:
Code:
$gameMap._interpreter._params = [loot, incDec, constVar, value];

which is the correct format for the array, but for some reason you're setting it to a new variable inside of the interpreter object. I dunno why you're doing that, but the code should work correctly if you call:
Code:
$gameMap._interpreter.command126([loot, incDec, constVar, value]);
Yes. If a function is expecting multiple variables to be passed as arguments, its prototype declaration would look like you have above. However, command126 does not look like that. The declaration in rmmz_objects.js says: Game_Interpreter.prototype.command126 = function(params)
So it is only expecting one variable to be passed in. If you look on the following lines of code, you see references to params[0], params[1], params[2] and params[3], so that one variable is expected to be an array with 4 elements in it.

You seem like you kind of know this, because the line above in your code says:
Code:
$gameMap._interpreter._params = [loot, incDec, constVar, value];

which is the correct format for the array, but for some reason you're setting it to a new variable inside of the interpreter object. I dunno why you're doing that, but the code should work correctly if you call:
Code:
$gameMap._interpreter.command126([loot, incDec, constVar, value]);
It Works!!!
thanks for the help
Just a note: I wouldn't recommend defining new functions in a script call like that, especially not on shared objects like Array. At best, your Array#random method will only last the rest of the play session. Instead you could do something like this:
JavaScript:
const pool = [75, 77, null, ...etc];
const loot = pool[Math.randomInt(pool.length)];
Also, you can use the this keyword to reference the current interpreter in a Script command, i.e. this.command126(etc). Probably not necessary in this case, just thought I'd mention it~

You seem like you kind of know this, because the line above in your code says:
Code:
$gameMap._interpreter._params = [loot, incDec, constVar, value];

which is the correct format for the array, but for some reason you're setting it to a new variable inside of the interpreter object.
That's for RMMV, I'm guessing they got confused with MV/MZ script calls.
That's for RMMV, I'm guessing they got confused with MV/MZ script calls.
Ah, thanks. I've never looked at any of the event command functions in MV, so I didn't know they referenced a separate member variable.

Original thread: https://forums.rpgmakerweb.com/threads/calling-interpreter-command126-via-script-call.146295/