
You can assign such a default value to a parameter using the equal ( =) sign, making it an optional variable: However, you can also explicitly define a default value for an argument which will be used instead of undefined when it is not passed in. You can use this to define optional arguments, and check whether an argument is passed in or not by checking if it is equal to undefined. If an argument is not given to a function, its value will be undefined. Var _mouse_dir = point_direction(x, y, mouse_x, mouse_y) move(4, _mouse_dir) Optional Arguments It can now be called like any runtime function and arguments can be passed into it: This function takes two arguments and applies their values to the instance's speed and direction variables. You can define your own parameters/arguments for a function, which will be available to the function as local variables and can be used for any purpose within that function: One will be classed as a "number" - since it returns a script index ID - and the other will be classed as a "method". NOTE: You can check this by using both forms in a script and then calling the runtime function typeof() on each of them.

Using the second form will instead be generating a global scope method variable, and as such it will not be recognised as a script function by the IDE and will require the use of the global prefix when being referenced in your code. In general, however, you would use the first form for script functions as it will define the function as specifically being a script function, meaning that it will be global in scope, be assigned a script index, and not require the global prefix to identify it since the compiler will recognise it as a script function. When creating a script with functions in it, the functions must be created using the following formats: Scripts can also be very handy from an organizational point of view, as they permit you to create groups of functions that belong to a certain category - for example, you might have several functions all related to collisions between instances in your game, so you would create a " Collision_Functions" script and store all these functions together in it.

Using scripts to define functions means that you can change the function just once when required and the change will be "picked up" by every object that has a call to the function. Script functions should generally be used if you have a block of code that you use in more than one place or object, or when you want a block of code to be used across multiple objects in a modular fashion. The functions you define in a script can resolve expressions, return values or do anything else that the GameMaker Language permits, just like the built in runtime functions. Script assets are essentially a collection of one or more user defined functions or variables that you write yourself as snippets of code in the Script Editor.
