SOLVED (Solved in 2.3.1) Need help on how to unpack an array to use as arguments in a function

Evanski

Raccoon Lord
Forum Staff
Moderator
This is a job for big brains, mine is smooth so that is why me is ask for the big helps.

Heres the walkthrough
I get a script name and arguments for that script like so
[scr_scriptname] [argument_array_here]

The first set of brackets hold the script name in a string that is converted to the asset name that is called in the script_execute function.

The second set of brackets is a makeshift array that is converted to a real array, and looks like so
String: (argument1,argument2,argument3) -> GML: arugs_array = [argument1,argument2];

ISSUE 1:
I will not know how long the argument array is
Solution 1:
I can check the arrays length and return a number for how long it is and use it as how many arguments to add to the function, for example sake lets say 3

ISSUE2:
I wont know anything about the script to be ran besides its name.

MAIN ISSUE:
How do I write:
Script_execute( script_name, [ARGUMENTS HERE FOR THE SCRIPT] )
the way I see it the array needs to be broken into variables, but like i said I can only get how many there are and what each is
Then I need to have the function be called with the arguments broken up between , to make the function work , I can not write a switch statement for every length of arguments there could be for a script being given.

So im looking for a way to have the arguments array be unpacked and put into the function like so
GML:
arugs_array = [arg_1, arg_2, arg_3];

vvvvvvvvvvv

script_execute( scr_pineapples, arg_1, arg_2, arg_3);
 
Last edited:

Bulletech

Member
ah so you have an array with the script name and the arguments for that script and you want to call it not knowing how many arguments there will be.
Can't you just pass the arguments array as an argument in script_execute and unpack it in the function called?

GML:
//call the function/script scr_pineapples
script_execute( scr_pineapples,argumentsArray);

//the function called scr_pineapples
function scr_pineapples(_argumentsArray) {
    for (var _i=0; _i<array_length(_argumentsArray); _i++) {
        // do something with _argumentsArray[_i]
    }
}
 

Evanski

Raccoon Lord
Forum Staff
Moderator
ah so you have an array with the script name and the arguments for that script and you want to call it not knowing how many arguments there will be.
Can't you just pass the arguments array as an argument in script_execute and unpack it in the function called?

GML:
//call the function/script scr_pineapples
script_execute( scr_pineapples,argumentsArray);

//the function called scr_pineapples
function scr_pineapples(_argumentsArray) {
    for (var _i=0; _i<array_length(_argumentsArray); _i++) {
        // do something with _argumentsArray[_i]
    }
}
ah I also forgot to mention, i wont know anything about the script besides its name
 

FrostyCat

Redemption Seeker
Starting with 2.3.1, there will be a script_execute_ext function that will do what you're looking for. But with GMS 2.3.0, you will have to use an adapted version of this:
GML:
function script_execute_array(scr, args) {
    switch (array_length(args)) {
        case 0: return script_execute(scr);
        case 1: return script_execute(scr, args[0]);
        case 2: return script_execute(scr, args[0], args[1]);
        case 3: return script_execute(scr, args[0], args[1], args[2]);
        case 4: return script_execute(scr, args[0], args[1], args[2], args[3]);
        case 5: return script_execute(scr, args[0], args[1], args[2], args[3], args[4]);
        case 6: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5]);
        case 7: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
        case 8: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
        case 9: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
        case 10: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
        case 11: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]);
        case 12: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11]);
        case 13: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12]);
        case 14: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13]);
        case 15: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14]);
        default: show_error("Too many arguments to script_execute_array().", true);
    }
}
 

Evanski

Raccoon Lord
Forum Staff
Moderator
Starting with 2.3.1, there will be a script_execute_ext function that will do what you're looking for. But with GMS 2.3.0, you will have to use an adapted version of this:
GML:
function script_execute_array(scr, args) {
    switch (array_length(args)) {
        case 0: return script_execute(scr);
        case 1: return script_execute(scr, args[0]);
        case 2: return script_execute(scr, args[0], args[1]);
        case 3: return script_execute(scr, args[0], args[1], args[2]);
        case 4: return script_execute(scr, args[0], args[1], args[2], args[3]);
        case 5: return script_execute(scr, args[0], args[1], args[2], args[3], args[4]);
        case 6: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5]);
        case 7: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
        case 8: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
        case 9: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
        case 10: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
        case 11: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]);
        case 12: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11]);
        case 13: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12]);
        case 14: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13]);
        case 15: return script_execute(scr, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14]);
        default: show_error("Too many arguments to script_execute_array().", true);
    }
}
Thank you so much! I had no idea there was a new function for this in 2.3.1, I'll just wait until that to use it, as this argument switch looks evil. (lol)

Thanks @BulleTech Studios & @FrostyCat
 

Bulletech

Member
haha yeah the switch statement looks a bit spooky and its not Halloween anymore so cant let it slide... 2.3.1 has quite a lot of things I think most of us are eagerly awaiting!
 
Top