Skip to content
Vadim Dyachenko edited this page Dec 18, 2020 · 5 revisions

GMS≥2.3 note: This GMEdit feature has been superseded by native named argument support and ??= operator.

General idea

#args magic allows to largely streamline the process of writing code that assigns arguments into temporary variables.

Upon saving, #args line is expanded into equivalent GML code.

Upon loading, GML code in matching format is collapsed into an #args line.

The feature can be enabled/disabled in preferences.

Examples of use follow

Normal arguments

If all arguments must be specified, write them as a comma-separated list, much like with var:

#args a, b

which will produce

var a = argument0, b = argument1;

Default values

If some of the arguments are optional and should be replaced by a default value, you can specify that by assigning a value to the named argument - again, much like with var syntax.

One restriction here is that the values may not span multiple lines.

#args a, b = 4

which will produce

var a = argument[0];
var b; if (argument_count > 1) b = argument[1]; else b = 4;

on GMS1 and

var a = argument[0];
var b = argument_count > 1 ? argument[1] : 4;

on GMS2.

"Undefined" default value

If you specifically want an argument to default to undefined if not provided, you can prefix it's name with a question mark ? instead of writing arg = undefined:

#args a, ?b

which will produce

var a = argument[0];
var b; if (argument_count > 1) b = argument[1]; else b = undefined;

on GMS1 and

var a = argument[0];
var b = argument_count > 1 ? argument[1] : undefined;

on GMS2.

Leaving space for custom arguments

If you intend to handle some of the arguments yourself (such as with arbitrary number of trailing arguments), you can tell #args to use argument[#] instead of argument# by leaving a comma in the end of the arguments-list:

#args a,

which will produce

var a = argument[0];