GML Overview: Accessing Variables Belonging To Other Instances

As described earlier, you can set variables in the current instance using statements like

x = 3;

But in a number of cases you want to address variables in another instance. For example, you might want to stop the motion of all balls, or you might want to move the main character to a particular position, or, in the case of a collision, you might want to set the sprite for the other instance involved. This can be achieved by preceding the variable name with the name of an object and a dot. So for example, you can write:

ball.speed = 0; This will change the speed of all instances of object ball. There are a number of special "objects".

self: The current instance for which we are executing the action

other: The other instance involved in a collision event

all: All instances

noone: No instance at all (sounds weird probably but it does come in handy as we will see later on)

global: Not an instance at all, but a container that stores global variables

So, for example, you can use the following kind of statements:

other.sprite_index = sprite5; all.speed = 0; global.message = 'A good result'; global.x = ball.x; Now you might wonder what the last assignment does when there are multiple balls. Well, the first one is taken and its x value is assigned to the global value.

But what if you want to set the speed of one particular ball, rather than all balls? This is slightly more difficult. Each instance has a unique id. When you put instances in a room in the designer, this instance id is shown when you rest the mouse on the instance. These are numbers larger than or equal to 100000. Such a number you can also use as the left-hand side of the dot. But be careful. The dot will get interpreted as the decimal dot in the number. To avoid this, put brackets around it. So for example, assuming the id of the ball is 100032, you can write:

(100032).speed = 0; When you create an instance in the program, the call returns the id. So a valid piece of program is

{   nnn = instance_create(100,100,ball); nnn.speed = 8; }

This creates a ball and sets its speed. Note that we assigned the instance id to a variable and used this variable as indication in front of the dot. This is completely valid. Let us try to make this more precise. A dot is actually an operator. It takes a value as left operand and a variable (address) as right operand, and returns the address of this particular variable in the indicated object or instance. All the object names, and the special objects indicated above simply represent values and these can be dealt with like any value. For example, the following program is valid:

{   obj[0] = ball; obj[1] = flag; obj[0].alarm[4] = 12; obj[1].id.x = 12; }

The last statement should be read as follows. We take the id of the first flag. For the instance with that id we set the x coordinate to 12.

Object names, the special objects, and the instance id's can also be used in a number of functions. They are actually treated as constants in the programs.

Previous: Local and Global Variables

Next: Arrays

Back To GML For Non-Programmers