Addressing Variables in Other Instances

Here you can find various methods for addressing variables in other Instances.

In the sections dedicated to variables you found out how to create and create and use variables within a single instance, or on a global scope, but what happens if you want to one instance to access a variable in another, different one? There are many cases when you may want to do this, for example in a collision with a bullet object, you may want to find out how much damage the bullet does by accessing a variable in the bullet, or you might want to stop the motion of all the balls in a puzzle , or you might want to move the main character to a particular position, or any number of other situations you typically come across in any game. Thankfully the GameMaker Language comes equipped with mechanisms to achieve this.

One of the most common methods for accessing or changing a variable in another instance is to use its object name as an identifier and then use a point "." to tell GameMaker that the variable used after is to be assigned or changed in that object. In practice it would look like this:

GML Code Example

obj_ball.speed = 0;

With the above code you are setting the speed of an instance of "obj_ball". However if you have more than one instance of the given object in the room, then it will apply to ALL of them equally - unless you are using one of the JS targets or HTML5, in which case it will affect only one, but you have no way of knowing which one it will affect - so if you need to access all instances of an object, you should be using with, as that is 100% cross platform compatible. In general, this format should only be used when you have a single instance of the object in the room, or (as you will see in the next part) when you have a specific instance ID.

You can also access a single instance of an object when there are multiple instances within the room using the instance id to tell GameMaker: Studio exactly which instance we wish to address. The instance id is the unique identifying number that is given to each and every instance created in your game. When you put instances in a room in the room editor, this instance id is shown at the bottom of the screen when you hover the mouse cursor over the instance, but even if you create an instance through code it still has its unique id number. These numbers are always larger than or equal to 100000, and such a number can also be used as the left-hand side of the point.

Note: The dot will get interpreted as the decimal point in a real number normally, so, to avoid this, put brackets around it!

The following code is an example of how this should be written:

GML Code Example

(100012).speed = 0;

Note that you cannot use the special keyword "all" with this method to target all instances, but you can use the keywords "other" and "self" without issues. You can also use variables, as long as the variable in question has stored a valid instance id. The following examples illustrate this...

Using a variable to set an instance value:

GML Code Example

var inst = instance_position(mouse_x, mouse_y, all);
 if instance_exists(inst)    {
    inst.speed = 0;

Note that the above code has an instance_exists() call in the code block. This is because using the point method to access or change another instances value will give an error and crash the game if the instance does not exist. Therefore if there is the possibility that the instance could be destroyed, deactivated, or otherwise removed from the room while using this method, you should always check before hand using the instance_exists() function or the instance_number() function.

These are all perfectly valid ways of reading, changing and setting variables in other instances, and work because the point is actually an operator. It takes a value as left operand and a variable (address) as the right operand, and returns the address of this particular variable in the indicated object or instance. All the object names, constants, IDs etc... simply represent values and these can be dealt with like any other value.

Next: Keywords

No Examples Submitted

Does this page need better examples? Be the first to Submit

No Edits Suggested

Is this page unclear or not descriptive enough? Suggest an edit to the page and with enough upvotes your changes will be made.

No Questions Have Been Ask

Do you have a question about this page? Ask it Here