GML Overview: Variables
From YoYoGames Wiki
What are variables? Variables are used in all programming languages, but I'll stick to the GM-context.
Contents |
Memory
Variables are really just cells in the RAM memory. You can use them to remember certain values. These values can be real numbers or text (ASCII-characters). Each variable has its own unique identifier. This identifier is used by GM to trace the right location in the memory. We see this identifier as the name of the variable.
GM variables
GM has some predefined variables for its own use. Examples are the variables speed, direction, x, y.... Most of these predefined variables can be changed manually, using the Set Variable action.
You can also make your own variables. If you use a Set Variable action with a self-made variable name (i.e. my_own_variable, or HP), the location in the memory is reserved for this variable and it can be read from and written to.
Use of variables
To check the value for a variable, you can use the Test Variable action. The name you give there (in the "variable" field) will be used to trace the right location in the memory and compare the value that is found there to the value you gave in the "value" fill-in. You can also draw the value of a variable on the screen, if you place the Draw Variable action in a Draw event of some object.
If you use a Move Free action, GM translates it to code when it creates the game. In this code, the variables speed and direction are set to the values you gave in the fill-in fields of the action. So in fact, you could use these variables to check which direction you're currently running: Move Free: speed: 5, direction: 270
- Variable speed is set to 5
- Variable direction is set to 270
That's what variables are.
GM has three different kinds of variables:
Local Variables
Local variables are present in every instance of every object. For example, every instance has its own current location. This location is stored in the local variables x and y. Local variables are read and set by either simply their name (i.e. x) or with the id if the instance (or object) as prefix (i.e. obj_enemy.x or other.x (other in a collision event)). The prefix can be used to read or write values in variables of other instances.
So you can have many variables that have the same variable name, but they are stored at a different location in memory. To choose the right location, GM has to know which instance the variable belongs to. The default choice is the variable that belongs to the instance in which the code is executed. If you use the code x=0, the horizontal location of the current instance is set to 0.
For an example see here: Local variable
Global Variables
Every global variable only exists at one location in memory. If you have a global variable named "HP", the location in the memory will allways be the same one, no matter which instance tries to read it. A global variable can only have one value at a time. GM has some predefined global variables, such as health, or room (the current room). These variables are remembered untill you shut the game down, so you can use them across different rooms. There is only one "health", that's why you can use it for one character only. You can't use the health-actions for a multiplayer game.
If you want to have your own global variables, you need a special keyword to tell GM it's a global variable. This keyword is ... global. If you use it as a prefix, the variable will be made global: Set Variable: variable: global.HP, value: 100 This HP-variable can be read and set from within every object, in every room.
Suppose you'd like to make a 2 player game, you could make 2 global variables to keep the health of these characters:
- global.HP1
- global.HP2
A second example of the use of these variables is an inventory:
- global.pebbles
- global.arrows
- global.gold
- global.health_packages
...
A second way of defining global variables is this: globalvar pebbles, arrows, gold, health_packages; After this code, you can use the variables without the prefix global, just like predefined global variables such as health.
For an example see here: Global variable
Temporary script variables
You can define variables for use within a script only. After the script or code ends, they will be deleted from memory. They are created using the var statement. Here's an example: Quote
//code to switch locations in a collision event //define the temperary variables var xx,yy; //remember the location of this instance in the temporary variables xx = x; yy = y; //overwrite the location of this instance (would be lost if we didn't use the previous code) x = other.x; y = other.y; //put the other instance at the location that was stored in the variables other.x = xx; other.y = yy;
Tips
- Never use spaces in names of variables. You can use letters, numbers and the "_" character. The "-" character will be seen as a minus-symbol, so you'd better avoid this one too.
You should also do this for the names of objects, sprites, sounds... in GM.
- Don't use the predefined GM-variables for your own purposes if you don't know exactly what this will lead to. You can check which variables are not to be used, if you press "Scripts => Show Built-in Variables" in GM. Check these if you're not sure. You can also type the variable in the index of the help-file (F1), but this doesn't contain all variables. It does contain an explanation of all but the deprecated variables.
- If you have too many global variables, and they use up too much memory, a good way to solve this is to keep global variables as local variables in a controller object. Just make sure to keep the controller object persistent, and when you need to set/read a variable, just use "controller.variable" for example.
See Also
- Previous: A Program
- Next: Assignments
- Back to: GML For Non-Programmers

