GML Overview: Global and Local Variables

You create new variables by assigning a value to them (no need to declare them first). If you simply use a variable name, the variable will be a local variable, stored with the current object instance only. So don't expect to find it when dealing with another object (or another instance of the same object) later. You can also set and read the local variables of other objects by putting the object name with a dot before the variable name.

To create global variables, that are visible to all object instances, precede them with the word global and a dot. So for example you can write: if (global.doit) {   // do something global.doit = false; }

Alternatively you can declare the variables as being global. This declaration looks as follows.

globalvar, , , ...;

Once this declaration has been executed, the variable is always treated as global without the need to put the word global and a dot in front of it. It only needs to be declared once in a piece of code that is executed. After that in all other places the variable is considered as being global.

Sometimes you want variables only within the current piece of code or script. In this way you avoid wasting memory and you are sure there is no naming conflict. It is also faster than using global variables. To achieve this you must declare the variables at the beginning of the piece of code using the keyword var. This declaration looks as follows.

var, , , ... ;

For example, you can write: {   var xx,yy; xx = x+10; yy = y+10; instance_create(xx,yy,ball); }

Previous: Expressions

Next: Addressing Variables of Other Instances

Back To GML For Non-Programmers