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 Studio 2 that the variable used after is to be assigned or changed in that object. In practice it would look like this:

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 unique instance name to tell GameMaker Studio 2 exactly which instance we wish to address. The instance name constant is the unique identifying constant that is given to each and every instance added to a room in your game. When you put instances in a room in the room editor, this instance name is shown in the instance properties window (shown when you double-click on an instance in the editor), and can be used as the left-hand side of the point:

inst_4DB70D2.speed = 0;

You can also use variables on the left of the point, as long as the variable in question has stored a valid instance id. The following examples illustrate this.

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

// Example 2
var new_inst = instance_create_layer(random(room_width), random(room_height), "Enemies", obj_E_Parent);
new_inst.direction = point_direction(new_inst.x, new_inst.y, x, y);
new_inst.target = id;

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. For example, using other in a collision event:

// Example 3 other.hp -= 10;
if other.hp <= 0
   {
   other.sprite_index = spr_E_Dead;
   }

Note that the above code in Example 1 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, and there is the possibility that this is the case. We don't need the check however in Examples 2 and 3 because we know that the instance is there since in Example 2 we created it, and in Example 3, its the other instance in a collision event. However, if there any 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 the 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.