• 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!

GML Getting number of arguments outside of a script?

Ido-f

Member
Is there a way to get the number of arguments a script expects without entering it and using argument_count?

I'm working on a stack machine (mainly to execute scripts through different game steps without using alarms) and getting that information would help with handling the scripts arguments without having custom code for it in each script (which probably means code duplication).
Other elegant ways to achieve the same purpose are also welcome.


Edit: thinking about it more, the stack machine thing isn't worth it probably. Leaving the thread here because the question is kind of interesting by itself.
 
Last edited:

Simon Gust

Member
Scripts don't expect any arguments until it finds the argument variables inside itself.
Example
Code:
return (argument0 * argument1);
expects 2 arguments because the code is guaranteed to run.
argument_count however only reads how many arguments have been passed.

This script:
Code:
var arr = 0;
for (var i = 0; i < argument_count; i++) {
   arr[i] = argument[i];
}
return (arr);
expects as many arguments as you put into it.
If you put no argument into it, the loop wont run, the script doesn't read any argument variables and therefore doesn't expect any.

If you have more specific arguments, you can test argument_count before any arguments are read.
Code:
switch (argument_count)
{
   case 2: return (argument[0] * argument[1]); break;
   case 3: return (argument[0] * argument[1] + argument[2]); break;
   case 4: return (point_distance(argument[0], argument[1], argument[2], argument[3])); break;
}
 
Last edited:

Ido-f

Member
If I understood correctly, when you use argument0 rather than argument[0], the script will throw an error if it hasn't received the precise amount of arguments as it uses, which made me think you could dynamically call it using that information.

But then I don't know how to dynamically insert arguments into script_execute anyhow, so I guess I should find a different way to do it.
 

Simon Gust

Member
If I understood correctly, when you use argument0 rather than argument[0], the script will throw an error if it hasn't received the precise amount of arguments as it uses, which made me think you could dynamically call it using that information.
Yes, you can read this here. I found an old thread:
https://forum.yoyogames.com/index.php?threads/difference-between-argument0-and-argument-0.5546/
Seems that if you use argument0 instead of argument[0] and so on, the script checks the validness at compile.
 
Top