MZ How do I return an array from a @arg?

Hello,

I'm building a plugin for my game and I was wondering (I haven't found an answer in the docs 😢).
When I want to use an array in the @arg section of the script, to add multiple variables to that argument (more like, being able to add or delete some in the plugin command) I have to declare it's type as @type variable[].
In the module, it works great, I can now add or remove variables to said argument.
Spoiler: Image
test2.png
But the problem I encounter now is, I can't seem to retrieve data from that argument.
The logical thing to do (to me) is to call for
JavaScript:
$gameVariables.value(args.fieldArray[0])
However, the console is showing me a value of 0. 😢

I gave a random value to all 3 called variables, but the console still shows a 0.
Spoiler: Image2
test.png

I tried with and without index, but it gives me the same result.

Can someone please help me out on this one?
It would be so great to be able to call different array of variables on seperate plugin commands with the same script.

Thanks for your help. 👋
Hello,

I'm building a plugin for my game and I was wondering (I haven't found an answer in the docs 😢).
When I want to use an array in the @arg section of the script, to add multiple variables to that argument (more like, being able to add or delete some in the plugin command) I have to declare it's type as @type variable[].
In the module, it works great, I can now add or remove variables to said argument.
Spoiler: Image
But the problem I encounter now is, I can't seem to retrieve data from that argument.
The logical thing to do (to me) is to call for
JavaScript:
$gameVariables.value(args.fieldArray[0])
However, the console is showing me a value of 0. 😢

I gave a random value to all 3 called variables, but the console still shows a 0.
Spoiler: Image2

I tried with and without index, but it gives me the same result.

Can someone please help me out on this one?
It would be so great to be able to call different array of variables on seperate plugin commands with the same script.

Thanks for your help. 👋
just based on your screenshot information... it's because when you [console.log] the variable still not changed yet... thus will print default value... thus 0

where did you randomize your variable value?
try entering the game... create an event that change that variable using event command... then use script call and console.log it there... does it also show 0?

also i'm not sure on how you grab the plugin parameter in your plugin (the screen shot did not show that)... if you grab it wrongly then it could also referencing to wrong variable and thus resulting in 0.
just based on your screenshot information... it's because when you [console.log] the variable still not changed yet... thus will print default value... thus 0

where did you randomize your variable value?
try entering the game... create an event that change that variable using event command... then use script call and console.log it there... does it also show 0?

also i'm not sure on how you grab the plugin parameter in your plugin (the screen shot did not show that)... if you grab it wrongly then it could also referencing to wrong variable and thus resulting in 0.
I gave the values before calling the command in an event.

Event =
Set value for 100,101,102
Call command that gets the array "100,101,102"
Show a console.log after the treatment.


"How do you grab the value"
I'm not sure, as this part I didn't make myself, but I think you mean this :

JavaScript:
if (Utils.RPGMAKER_NAME === "MZ") {
    Game_Interpreter.prototype.command357 = function(params) {
        params[3].interpreter = this;
        PluginManager.callCommand(this, params[0], params[1], params[3]);
        return true;
    };
    Game_Variables.prototype.setValueEx = function(variableId, value) {
        if (variableId > 0 && variableId < $dataSystem.variables.length) {
            this._data[variableId] = value;
            this.onChange();
        }
    };
}

It works for singular variables, when I try to retrieve a variable, I'm simply doing :

JavaScript:
 * @arg variableID
 * @text ID of the variable to get data from
 * @type variable
 * @default 1
 * @desc Select the ID of the variable
 *

variableData = $gameVariables.value(args.variableID)
console.log('data :',variableData)

But it doesn't work when I'm trying to apply an array of variables into an argument.

I'm not very experienced, so I'm not even sure where I'm doing things wrong. 🙁
Thanks for your answer though. 😊
Moved to: Learning Javascript
When using arrays, the Core Code gives you a String containing the items, again as String.. bit confusing, but can be solved easily by writing

Code:
JSON.parse(args.variableIds).map(arg => Number(arg))

Example:

Code:
PluginManager.registerCommand("Plugin Name", "Function Name", args => {
    const variableIds = JSON.parse(args.variableIds).map(arg => Number(arg));
    
    for (let variableId of variableIds) {
        alert("The value of ID " + variableId + " is " + $gameVariables.value(variableId));
    }
})
When using arrays, the Core Code gives you a String containing the items, again as String.. bit confusing, but can be solved easily by writing

Code:
JSON.parse(args.variableIds).map(arg => Number(arg))

Example:

Code:
PluginManager.registerCommand("Plugin Name", "Function Name", args => {
    const variableIds = JSON.parse(args.variableIds).map(arg => Number(arg));
   
    for (let variableId of variableIds) {
        alert("The value of ID " + variableId + " is " + $gameVariables.value(variableId));
    }
})
Exactly what I needed, thank you very much.
I'm not sure exactly how though, which doesn't teach me alot, but it solves my issue. 😤


Moved to: Learning Javascript
I'm sorry for the wrong forum, it's as confusing as javascript sometimes. 😊
I can be more detailed with you. The Core code does not return the array itself, but instead a String representation of a String array (don't ask me why), so instead of [1, 2, 3] you get "["1", "2", "3"]", that means we need to convert it two times.

JSON.parse(string) takes the string and converts it into JSON objects, and map(arg => Number(arg)) returns a new list in which all items are converted into Numbers.
@Aerosys already explain it clearly... but to expand a little bit on that...
mainly because in javascript using [0] after a string object did not throw error... but will grab the character at that index instead...
ex:
Code:
var text = "whatever"
console.log(text[0]) # => this will print letter "w"
console.log(text[1]) # => this will print letter "h"
console.log(text[2]) # => this will print letter "a"

and just like @Aerosys said when you grab the plugin parameter... it will be in string format...
so you must convert it to the format of what you want... and JSON.parse(string) will convert what inside the bracket into an object.
ex:
Code:
var text ="[1,2,3,4]"
var array = JSON.parse(text) #=> will become array [1,2,3,4]
console.log(text[0]) #=> will print "["
console.log(array[0]) #=> will print 1

hope this help
Thanks alot for the explanation. @Aerosys @estriole 😤
It's very clear now. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-return-an-array-from-a-arg.136658/