variable argument scripts

Pfap

Member
I have been over the manual sections about scripts and passing arrays to scripts. Maybe I missed something, but I'm making this posting not because I'm having trouble finding a solution I already know of several work arounds. I'm making this posting, because I want to finally figure out how to get a variable number of arguments working with scripts cleanly.

Here's the relevant code.

Script create_popup:
Code:
show_debug_message(argument_count);

if argument_count == 5{
 
var xpos = argument[0];
var ypos = argument[1];
var text = argument[2];
var button_amount = argument[3];
var b_string = argument[4];
show_debug_message(b_string);
}

 
if argument_count == 6{
 
var xpos = argument[0];
var ypos = argument[1];
var text = argument[2];
var button_amount = argument[3];
var b_string = argument[4];
var c_string = argument[5];
show_debug_message(b_string);
show_debug_message(c_string);
}
And here is how I am calling it:
Code:
//create an array with desired amount of arguments
var args = [room_width/2,room_height/2,"Are you sure?",1,"Ok"]
create_popup(args);
This proceeds to crash, because none of my var's are being set in the beginning of the script. Also, the argument_count variable is displaying 1 in the console output. Which I take to mean that the array of 5 arguments is being accepted as 1 array.


This is the portion of the manual that has me confused on how to use the built in argument[n] array.

However you can supply a variable number of arguments to a script using the built in argument array, and you are not limited to a maximum of 16 arguments either when using this array, but instead can have as many as you require (although, again, you must ensure that all arguments are referenced within the script):

argument[0 ... max_num]

When passing in a variable number of arguments as an array of values you can use the following function:

This can be used to find out how many arguments have been passed and adapt the script to use only those arguments supplied.

NOTE: You cannot mix the two types of argument variables when calling a script. You must use either argument0...15 or argument[n].
Scripts can also return a value, so that they can be used in expressions. For this you would use the return statement:


tl;dr

How do you use the built in argument[n] array to create variable argument scripts?
 

Pfap

Member
I think you are sending in an array to the script. That is fine, but that is why the debug message says that there is only 1 argument. The "if argument_count == 5 will not run.
The manual says
When passing in a variable number of arguments as an array of values you can use the following function:

I am passing in a variable number of arguments as an array of values.
 

8BitWarrior

Member
The argument[] array is created automatically when you pass multiple arguments to a script. You do not pass an actual array to make use of this.
So, if you had a script that added numbers together, you could have this...
value = AddNumbers(2, 5);

Code:
/// AddNumbers(a,b)
var val1 = argument0;
var val2 = argument1;
var result = val1+val2;
But if you wanted it to support a differing number of arguments, you could change it like so...
value = AddNumbers(2, 5, 10);
Code:
/// AddNumbers(a,b,...)
switch(argument_count)
{
    case 2: return argument[0] + argument[1];
    case 3: return argument[0] + argument[1] + argument[2];
    case 4: return argument[0] + argument[1] + argument[2] + argument[3];
}
Passing an array as an argument isn't working as you are expecting it to. I'll show how you would have to use an array if you passed it...
values = [2, 5, 10];
value = AddNumbers(values);
Code:
/// AddNumbers(valueArray)
var values = argument0;
switch(array_length_1d(values))
{
    case 2: return values[0] + values[1];
    case 3: return values[0] + values[1] + values[2];
    case 4: return values[0] + values[1] + values[2] + values[3];
}
I hope this makes the difference a bit more clear.
 

Pfap

Member
Yeah, the wording does make you think that you should send in the arguments as an array. But I am pretty sure you send in the arguments normally, and then you can retrieve them from the "argument[0], argument[1]" array.
Hmmm, I think I already tried that and it wasn't correct. I'll try again.

Edit:

Yea, that was it I must have had some other problem that caused me to think it wasn't working.
 

FrostyCat

Redemption Seeker
This is what you should have done:
Code:
create_popup(room_width/2, room_height/2, "Are you sure?", 1, "Ok");
About argument being an array --- this simply isn't true given my experience with it. In many ways it is a specially handled construct, not a true array. Though you can reference single entries from it like an array (e.g. argument[0]), there's nothing else you could do with it that you could do with a real array. Functions like array_1d_length() and array_copy() won't work on it, and you can't treat it as a standalone unit. By standalone unit, I mean situations like the following, all of which are currently illegal:
Code:
var args = argument;
Code:
ds_list_add(global.things, argument);
Code:
return argument;
 
Last edited:

Pfap

Member
The argument[] array is created automatically when you pass multiple arguments to a script. You do not pass an actual array to make use of this.
So, if you had a script that added numbers together, you could have this...
value = AddNumbers(2, 5);

Code:
/// AddNumbers(a,b)
var val1 = argument0;
var val2 = argument1;
var result = val1+val2;
But if you wanted it to support a differing number of arguments, you could change it like so...
value = AddNumbers(2, 5, 10);
Code:
/// AddNumbers(a,b,...)
switch(argument_count)
{
    case 2: return argument[0] + argument[1];
    case 3: return argument[0] + argument[1] + argument[2];
    case 4: return argument[0] + argument[1] + argument[2] + argument[3];
}
Passing an array as an argument isn't working as you are expecting it to. I'll show how you would have to use an array if you passed it...
values = [2, 5, 10];
value = AddNumbers(values);
Code:
/// AddNumbers(valueArray)
var values = argument0;
switch(array_length_1d(values))
{
    case 2: return values[0] + values[1];
    case 3: return values[0] + values[1] + values[2];
    case 4: return values[0] + values[1] + values[2] + values[3];
}
I hope this makes the difference a bit more clear.


Thanks, for the informative answer :)
 

Pfap

Member
This is what you should have done:
Code:
create_popup(room_width/2, room_height/2, "Are you sure?", 1, "Ok");
About argument being an array --- this simply isn't true given my experience with it. In many ways it is a specially handled construct, not a true array. Though you can reference single entries from it like an array (e.g. argument[0]), there's nothing else you could do with it that you could do with a real array. Functions like array_length_1d() and array_copy() won't work on it, and you can't treat it as a standalone unit. By standalone unit, I mean situations like the following, all of which are currently illegal:
Code:
var args = argument;
Code:
ds_list_add(global.things, argument);
Code:
return argument;

Your use of the term "alias" is new to me I'll need to do more research. But, if you do that with a normal array what happens? Are gamemaker arrays reference types?
Code:
var my_array = [1,2,3,4];

var other_array = my_array;

other_array[0] = 5;
Does the above make 2 separate arrays or does my_array[0] now equal 5?
 

8BitWarrior

Member
Gamemaker arrays are indeed reference types. Your example would create 2 arrays. You could use '@' to prevent a copy and write to the original array.

other_array[@ 0] = 5; // write to original array -- no copy made
 
Top