A basic description of a global variable is one that, once
declared, belongs to no instance in particular and yet can be
accessed by all. Just like local variables, global variables must
be declared, but unlike a local variable, a global variable remains
in memory until the end of the game. So, you can create a global
variable to keep track of (for example) the number of bullets that
the player has and then just update this variable at different
points in the game. A global variable does not belong to any
specific instance and can be accessed, changed and used by all
instances at any time, but any change made to the variable are
"global", in that all instances using the variable will be affected
by the change. Let's have a look at an example:
global.food = 5;
We declare the "food" variable by first writing "global" and then a
"." to tell GameMaker that this variable is now global scope. We
will need to use this method from now on any time we are required
to access or to change this variable in any way. So, we have
created a new variable called "food" and we have declared it as
global. Now, any instance can use and change this variable in any
way and all other instances will "see" this. For example we could
have a different food object that the player collides with and in
the collision event we have:
global.food +=1;
We also have another object that draws this value like this:
draw_text(32, 32, "food = " +
string(global.food));
With global variables we can change values and see those changes
reflected in all instances of the objects that reference this
variable. As with local variables you have to take care not
to name your global variables the same as any instance variables as
that will cause you problems and make bugs creep into your games
due to variable overlap, which can be a difficult issue to debug
sometimes. In general you should have a single script or object
that declares all your global variables at the very start of the
game (for example, in the Room Start Event), so that they are
initialised and ready for any instance to use, and this also gives
you a handy place to go back and reference should you need to check
a variable name.
GameMaker: Studio has a collection of "built in" global
variables too, so you should be aware of them as you may name one
of your instance variables the same or wish to have your own global
variable with the same name and wonder why you are getting errors!
They are easy to spot, however, as they are shown in a different
colour in the code editor and also come up in the auto-complete bar
at the bottom. the majority of built in global variables are very
specific and will only be used on rare occasions, but there are
three in particular which are very useful and you can find out more
about them from the pages listed below:
The following method can also be used to declare global
variables, but it is only included for backwards
compatibility, and it is not recommended that you use this
method for new projects as future versions of GameMaker:
Studio may not support it.
The second method for creating global variables is to declare them
as such using the globalvar declaration, much as you would
a local variable using the var declaration. For
example:
globalvar food;
food = 5;
Once declared in this way that variable is now considered global
and requires no "global." prefix - which also means that it's a lot
harder to identify global variables in your code and it's also much
easier to get variable overlap as you use the same variable name in
different objects - and the variable is accessed as follows:
food += 2;
or:
draw_text(32, 32, "food = " +
string(food));