• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [TEXT TUTORIAL] object.variable vs global.variable vs globalvar/macro structs

xDGameStudios

GameMaker Staff
GameMaker Dev.
GM Version: All
Target Platform: Windows / All
Download: n/a
Links: n/a

[PROBLEM]
What is the difference between object.variable, global.variable and what are globalvar macros?
When should I use one of the other?!


[EXPLANATION]
The first thing that we need to understand is that we are trying to accomplish something that is actually very similar, that is:

"We want to access a variable that is stored somewhere."

  • When we use object.variable: here variable is stored inside object
  • When we use global.variable: here variable is stored inside global

So let's look into each one of the example individually.


[OBJECT]
When we say object we are not talking about the literal object word, but a name of an object you create inside the IDE.
Here are some examples of objects you might have in you game objPlayer, objEnemy, objRock, etc.

These objects might have variables associated to them, like: level, hp, broken, dead, etc.
This probably means you will be wanting to store the variables inside them.
Your player might have hp but so will your enemy so your setup will have objEnemy.hp and objPlayer.hp.

There is one important question you may ask - "What if I have multiple instances of that same object in the room?"

For example imagine you have three objBoat with variable color (but each boat as a different color).
If we do objBoat.color this would access the color of ONE of those boats, the oldest one (the first being created).
So in other words if there is more than one instance GMS doesn't assure you that you will access the desired instance.


[GLOBAL]
Now here comes the plot twist.. 🤔 global is actually an "object" too.
It's an object that exists inside every room that has no events, it has no builtin variables, is not visible and it is persistent.
So for example global.hp means you are trying to access hp variable inside the global object.

When I say that global is an object it's because well.. it actually kind of is.
For explanation purposes let us create an object:

  1. call it my_global (for example)
  2. make it not visible,
  3. make it persistent
  4. make sure it has no events
  5. drop the object inside the first room of your game.

You just made your own custom "global" object. That's it, global can actually be represented as an object.

[GLOBALVAR/MACRO STRUCTS] (2.3+)
There is one third option regarding storing global data, this is possible only after GMS2.3 and requires using structs.
This approach is even closer to what global actually is as it doesn't require instantiation, it is not visible, doesn't have events and is already persistent.

Here are the setups:
GML:
// Setup ONE
globalvar MyGlobal; MyGlobal = {};

// Setup TWO
#macro Settings global._Settings
Settings = {};
These two will allow you to have a struct (GMS2.3+) where you can store any data you want:
  • The first setup will declare a globalvar to make sure the variable can always be accessed in your code and initialise it to a struct.
  • The second setup makes use of the internal global instance object and creates a macro that can be used to reference a struct inside it.
In both cases you could use that as you would to access data inside global using MyGlobal.variable or Settings.variable

NOTE: With both of this setups you can "easily" erase/overwrite the holding structure itself if you are not careful.
So doing MyGlobal = undefined; or Settings = undefined; is allowed and could break you game.


[USAGE]
So when should you use one or another?

If you want to store information that is going to be used across rooms (for example you have a list with the enemies in a room) and the information doesn't belong to anyone in specific (is just a list that can be used by enemies or by the player or by the game) then you can use global or macro/globalvar structs if on GMS2.3+.

Now imagine we are talking about hp if we have one variable called global.hp ... now that would not make much sense because we don't know who does it belongs to, it could be the player hp or the enemy hp.. or anything else. So hp is a variable that should belong to something so it's convenient to store it within that something.


[CONCLUSION]
Even though there is no actual rule that you MUST follow, that is something called good code structure that will help you maintain you code clean and readable.
So it all depends on the context of what you want to do. You can create a game without using global at all or create a game where EVERYTHING is stored in a global scope.


Here xD from xDGameStudios,
Good coding to you all.
 
Last edited:
Top