Variables And Variable Scope

Variables are the basic unit for most programming operations. Like any programming language, GML uses them.

They are used to store information in the device's memory for later (or instant) use, and given a name so you can refer to them in runtime and script functions.

A variable in GML can store many different Data Types, like a real number (e.g.: 100, 2.456575, -56, etc.), a string (e.g.: "Hello world!"), an integer (e.g.: 1, 556, -7), or a boolean (true or false), as well as other things:

var _num = 126.4545;
var _str = "Hello World";
new_num = _num * 100;
global.my_string = _str + " I said";

You can also use variables to hold the values returned from functions, for example:

var _id = instance_nearest(x, y, obj_Tree);
root = sqrt(1000);
global.str = string_upper("Hello World");

So, a variable is something that we can name and use to store a value for later use in one or more operations.

A good "real world" example of a variable is the speed of something, e.g. the speed of a car. A car's speed is clearly variable, because at some point in time the car will be standing still (i.e. its speed is equal to 0), at another point in time the car will be driving (i.e. its speed is greater than 0). When the car accelerates or slows down, its speed changes. So it makes sense that if you want to store the car's speed in a GameMaker game that you store it in a variable. In the game, you'd e.g. set the variable to 0 when the game starts and change it whenever you want the car to change its speed.

Variable Assignment

In GML, like in many programming languages, you first have to create a variable "assignment" before you can use it. This basically means that you tell the computer the name you wish to use for the variable and assign it an initial value. The variable is then given a place in memory to store the value or perform operations on it. A variable assignment takes the form:

<variable> = <expression>;

An expression can be a simple value but can also be more complicated, so, rather than assigning a value to a variable, you can also add a value to the current value of the variable using +=, for example:

a = 100;   // Assigning a simple value
b = 200;
c = 300;
a += b;    // Assigning with operation
a = b + c; // Assigning with expression

NOTE The GameMaker Language will also accept := for assignments, although this is not typically the most common way to do it:

<variable> := <expression>;

Similarly, you can subtract using -=, multiply using *=, divide using /=, or use bitwise operators using |=, &=, or ^=. You can also add or subtract one from a value using ++, --. For further information see the section on Expressions And Operators.

Note that you cannot do the following (or any variation):

a = b = c = 4;

And instead it should be done as:

a = 4;
b = 4;
c = 4;

Naming Rules

When forming variables in GML they must have a name that starts with a letter or the underscore symbol _ and can contain only letters, numbers, and the underscore symbol _ with a maximum length of 64 symbols. So, valid variable names are things like fish, foo_bar, num1, or _str, while invalid ones would be 6fish, foo bar, or *num.

You cannot use the names of your assets (e.g. Sprites, Scripts, etc.) for your variables, unless you specify an instance or struct (or keywords like globalself) before such a name, e.g. if you have a script called Script1, a variable called Script1 can be defined as self.Script1 = <value>; in an instance or a struct. Doing this explicitly defines the scope of such a variable. For more information, see the section below.

Variable Scope

Variable scope tells which part of your code a variable belongs to, it is determined by where you first define it in your code. Variable scope also tells from where in your code you can access it. If you need to access variables that are in a different scope, you can change the scope.

Changing Scope

You can access variables in a different scope in two ways: 

The variable a can be either an instance or a struct. You can also use one of the Instance Keywords or the global keyword.

See Addressing Variables In Other Instances for detailed information.

Types of Scope

By default, when you assign variables in Object Events, you assign them to instance variables, however there are actually four other main variable types when you program with GameMaker, each with its own scope.

The different kinds of variables and their scope are all outlined in the following pages:

NOTE Constants aren't "true" variables since they're replaced by their underlying value by the Compiler and don't exist as variables in-game.

Built-in Variables

The GameMaker Language has multiple different built-in variables that can have any of the above-mentioned scopes (except local). These variables are special as they are included by default as part of the objects and the rooms in the game world. Some built-in global variables are listed in the section mentioned above, and the different parts of the manual for sprites, rooms, objects, etc., also outline the built-in variables available in each case.

Examples of built-in instance variables are:

And examples of built-in global variables are:

Most built-in variables can be changed and set like other variables, and some can even be Arrays, only you don't have to set them to create them like you would a regular variable as they will already be initialised to a default value.

Variable Functions

Finally, there are a number of functions that are dedicated to setting, getting or checking variables in some way, available from the following page: