GML About variables and global variables


I wanna know about the difference between them, is that variables are attached to the object only, and you can't use it in another object and global variables are available across all objects? I don't know if I am right.


(Instance) Variables are attached to instance of object. So, if you have two instances of one object, each of them have OWN value of variable with same name.
Global variables mean that it's shared among everything in game, not only objects - it's also shared in room creation code for example. There can't be two global variables with same name.
There also "temporary" local variables, available from the moment they are defined (var xxx;) until end of current event (as they are local, can't be accessed from other instances). They can be used with with() statement without calling other., if they doesn't overlap with called instance local variables.

@Slyddar the link you shared is for GMS1 docs, it's not supported anymore.


The reason that I asked here is that I don't really understand the documentary, but I think I understand now, thanks!


@Slyddar the link you shared is for GMS1 docs, it's not supported anymore.
What link? :D hehe

The reason that I asked here is that I don't really understand the documentary, but I think I understand now, thanks!
Yeh it can be a bit confusing. I remember looking up some things in there, and having to read it multiple times to get it to sink in.

That's fine, glad someone was able to explain it. Next time mention you have tried reading the docs, or any other info, as it can help us to help you better :)


As an answer the one question that was not yet addressed, except maybe in the documentation:

All variables can be referenced by all instances.

First off, get global variables out of your head. LOL Think of global variables as belonging to a deactivated instance called "global". It functions exactly the same way as any other normal variable, and if you think of it as just another normal variable belonging to a hidden instance, then there should be less confusion.

If all instances can refer to variables in "global" by using global. in front of the variable's name, then likewise all instances can refer to variables inside another instance by putting that instance's id in front of its variables.

This needs to be expanded upon, however, since you can use the name of the object class the instance belongs to, you can use the ID of the object class the instance belongs to, you can use the ID of the instance itself, or you can use a variable which has the ID or object class saved in it. Which of these you use will affect how the variable is handled, potentially. Let's look at an example:

var inst=instance_create(x, y, oDog);

So the variable inst is a temporary/local variable. It will only exist for the duration of this code. On a side note, throughout the duration of this code, all instances we could reference in this code would have direct access to inst. It temporarily exists locally in this code.

The value of inst will be the id (that's an actual variable all instances in GM are automatically assigned) of the instance of oDog created. An Instance ID (not to be confused with the array instance_id) is an integer with a value of at least 1000001. This differentiates it from an object ID, which is an integer with a value of at least 0. (This will become clearer later.) For this example, we will go ahead and say the value of inst is 1000002 and the index of oDog is 4.

Let's say the object oDog has a variable breed defined in the Create Event. Any instance in the room can refer to our new instance's breed in various ways.


You'll notice that when using direct number values, you need to put parentheses around the number. (Maybe they got rid of this requirement in the latest versions, but I think it's still there.) Also, you rarely ever actually know the number itself, which is why you would typically use a variable or the object name.

Now, as for the object references, when you reference a variable which you proceeded with the name OR index of the object it belongs to, GM will behave differently. If you try to write to the variable, meaning you try to assign a value to it, that value will be assigned to every instance of that object in the room, creating a new variable if one does not already. However - and this is the most important reason why you should nearly always refer to variables through the instance rather than the object - if you try to read a variable that is referenced by its object, if there are multiple instances of that object in the room, a seemingly random one will be chosen. Now, it's not actually random, and you can take advantage of this with testing, but even I would advise against it.

There is one other major difference between referencing and variable. If you recall, at the top of this post I italicized "deactivated". Referencing variables via an object index will not Grant you access to deactivated instances of that object. If you know the actual id of an instance in refer to its variables with the id (for example, inst.breed), even if that instant has been deactivated, you still have access to all of its variables. The instance will not run any of its events, but it will still hold and grant access to its variables.