Scripts are an important part of GML and this section explains their use.

When you create a script, you want to access the arguments passed to it, either when using the script action, or when calling the script as a function from a program (or from another script). These arguments are stored in the built in variables argument0, argument1, ..., etc... up to argument15. So there can be at most 16 arguments passed through to a script when called from code using this method,and if you are using the script D'n'D action then only the first 5 arguments can be specified.

When using the argument0...15 variables for a script, you must ensure that the script uses all the supplied arguments. For example if you have a script that takes two arguments and you only supply one then you will get an error for that script. The same goes if you supply more arguments than you require. However you can supply a variable number of arguments to a script using the built in argument array, argument[0 ... max_num], where the "max_num" value is the last argument you are passing in. When passing in arguments as an array of values you can use argument_count to find out how many there are and adapt the script to use only those arguments supplied, and it's worth noting that you are not limited to 16 arguments when using this method of parsing the input arguments.

NOTE: You cannot mix the two types of argument variables when calling a script. You must use either argument0 ... argument15 or argument[0 ... max_num].

Scripts can also return a value, so that they can be used in expressions. For this end you use the return statement:

return <expression>

It should be noted that the execution of the script ends at the return statement, meaning that any code which comes after the return has been called will not be run. Here is a short example script called "scr_sqr" which calculates the square of whatever value is passed to it, and it includes error catching in case the argument that it is passed is not a real number:

if !is_real(argument0)
   return 0;
   return (argument0 * argument0);

To call a script from within a piece of code, just act the same way as when calling functions - that is, write the script name with the argument values in parentheses. So, the above script would be called like this:

if keyboard_check_pressed(vk_enter)
   val = scr_sqr(amount);

It is sometimes necessary for you to know how many arguments that a script has been passed from the code that calls it, and so GameMaker: Studio also provides the following read-only variable for this:


For more information on scripts please see Advanced Use - Scripts.

Back: GML Overview
Next: Comments in Code
© Copyright YoYo Games Ltd. 2018 All Rights Reserved