• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Q:Script & Arguments

Z

zendraw

Guest
Hi,
is it possible to use a various amount of arguments in a customly created script, like it is now in the instance_destroy(); function?

i have a script i want to use for various situations but the number of arguments vary. usually when i put more or less arguments it gives an error, but i remember at some cases it didnt give me an error to scripts that didnt use arguments yet i set arguments when i called them, becouse forgot to edit them out after changing the script`s functionality.

the way this script is going to function is, it will call another script if a case is met, and that other script wuld be of any kind and so its arguments wuld vary. so this script wuld be more or less universal.

using GMS1.
 

kburkhart84

Firehammer Games

Description

This read-only variable returns the number of "arguments" that are passed through to a script. Normally used in conjunction with an argument array (argument[0...15]) to permit varying input arguments for a given script.


Syntax:
argument_count;



Returns:
Real



Example:
var i, arg;
for (i = 0; i < 5; i += 1;)
{
if argument_count > i
{
arg = argument
}
else
{
arg = -1;
}
}

The above code uses the argument_count variable to initialize variables, and is an example of how to permit a script to accept a variable number of arguments.

This is from GMS2 documentation. If you access the arguments in your script using the array version, then those arguments are optional instead of required. And you would use argument_count in order to know how many arguments were sent(since your code would likely vary depending on that).

EDIT**********

Forgot to mention...I understand that GMS1 was the same as GMS2 in this.
 

poliver

Member
use argument_count to see how many arguments have been passed into the script

then do your logic

if argument_count that - do that
else do that and that etc.
 
Z

zendraw

Guest
thats cool, but the problem is this -> script_execute(script, argument1, argument2, argument3, ...);
how do i script_execute with various arguments without doing a chain of ifs?

note, the script that will going to be executed is the in the function im making, so it will have a set number of arguments.
 

kburkhart84

Firehammer Games
I'm understanding that script_execute is also a script that can have optional arguments. You just call it that way. Maybe there is something I'm missing in what you are doing though, I'd have to have more information. I'm sure there is a way to do what you want, maybe something different from what you currently have going.
 

poliver

Member
what are your arguments like? are they similar, same type?

if yes you can run a for loop
Code:
for (i = 0; i < argument_count; i++)
{
    execute code with argument[i] etc.
}
 
Last edited:

poliver

Member
again if you want to do different logic on the argument you can create multiple for loops and offset them

Code:
Code:
for (i = 0; i < 4; i++) //first 4 arguments for example
{
   execute code with argument[i] etc.
}

for (i = 4; i < argument_count; i++) //further unlimited number of arguments
{
    execute code with argument[i]
}
 

poliver

Member
also if youre using different types in arguments you can overload your scripts by using

is_real and is_string like
Code:
if is_real(argument0)
{
}
if is_string(argument0)
{
}
else
 
Z

zendraw

Guest
so, this is how it wuld work
Code:
FUNCTION:
scr_func(case1, case2, script, argument1, argument2, ..., argument5);

IN FUNCTION:
if (argument0 && argument1)
{
script_execute(argument2, argument3, argument4, ..., argument8)
}
so id need to define different number of arguments when im calling the function also, also i dont want to use loops, it wuld be called frequently and the loops will have an impact
 

Hyomoto

Member
So using script execute it is also possible to pass variable arguments. GM2, and GM:S to a lesser degree, has support for 'ragged arrays' which means that you can create an array of any size at any time. In the case of script execute:
Code:
var func = my_function;
var args = [ 0, 1, 2 ];

script_execute( func, args );
On the distant end you would receive an array with the arguments that you wanted, much like you would if you had used argument[] instead. In the case of the above it is possible to do something like:
Code:
/// @func my_function( argument0, argument1 );
var arg = [];

for ( var _i = 0; _i < argument_count; _i++ ) {
  arg[ _i ] = argument[ _i ];
}
script_execute( some_function, args );
While I'd argue that depending on what you are doing there's probably a better way than this, it does go to show with a little creativity you'll find GML has less limitations that you think. To borrow from the last example:
Code:
/// @func my_function( condition, condition, function, argument0, argument1 );
var arg = [];

for ( var _i = 3; _i < argument_count; _i++ ) {
  arg[ array_length_1d( arg ) ] = argument[ _i ];
}
if ( condition && condition ) { script_execute( function, args ); }
 
so, this is how it wuld work
Code:
FUNCTION:
scr_func(case1, case2, script, argument1, argument2, ..., argument5);

IN FUNCTION:
if (argument0 && argument1)
{
script_execute(argument2, argument3, argument4, ..., argument8)
}
so id need to define different number of arguments when im calling the function also, also i dont want to use loops, it wuld be called frequently and the loops will have an impact
Just a note here, when you are using a variable number of arguments and processing them via argument count, you need to use the argument array format (i.e, argument[0], argument[1] ...etc... ) within your script.

If you start using argument0, argument1, then those become expected arguments, you will lose the ability to pass in a variable number of arguments, plus you will get an error if you don't explicitly pass in the correct amount of arguments.
 
Z

zendraw

Guest
so the point is to use argument[x] instead of argumentx? and im guessing you can either use one or the other?
 
Top