Scripts

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, 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 ... 15]. 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.

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

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

GML Code Example

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:

GML Code Example

{
 if !is_real(argument0)
    {
    return 0;
    }
 else
    {
    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:

GML Code Example

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.





No Examples Submitted

Does this page need better examples? Be the first to Submit

No Edits Suggested

Is this page unclear or not descriptive enough? Suggest an edit to the page and with enough upvotes your changes will be made.

No Questions Have Been Ask

Do you have a question about this page? Ask it Here