Keywords


To make certain things easier in GameMaker Studio 2, you can use one of several keywords in your scripts and actions. These keywords are used primarily to identify instances and each one is explained in the text below. Note that all keywords are represented by a negative integer value internally, so care must be taken when assigning values to variables, as you may get unexpected results later as GameMaker Studio 2 interprets the value you have used as something else. You should also note that using the values instead of the keywords in your code is not at all recommended and could cause issues later on.

Keyword Description value
self The instance which is executing the current block of code.    -1

self can be used to identify the calling instance of the current block of code. It always returns the value of -1 which GameMaker Studio 2 interprets as the unique ID for the instance. For example:

var val = 100;
with (instance_create_layer(x, y, "Instances", obj_Fire))
  {
  self.val = val;
  }

In this example you can see that we have a local variable called val and we want it to set the instance variable with the same name in the newly created object instance. To identify the instance variable correctly and tell GameMaker Studio 2 to set it in the instance calling the code block, we use the self keyword. Note that we recommend using the id keyword in most situations like this over the self keyword as it is more explicit and obvious what exactly is happening.


Keyword Description value
other The other instance involved in a collision event, or the other instance from a with function.    -2

The special keyword other has two different ways that it can be used to reference a specific instance: when used in a with function (explained here) or when used in a collision event, which is what this section is going to explain.

A collision event can only happen between two instances. You can have multiple collisions between multiple instances, but they are all resolved by GameMaker Studio 2 on a 1-on-1 basis, with the instance that has the collision event and the "other" instance that is involved. Imagine you have a player object, multiple enemy objects and multiple bullet objects that the enemy can fire at you. You can assign each enemy a single bullet instance but with a different damage variable randomly assigned to it when created, for example:

var nnn;
nnn = instance_create_layer(x, y, "Bullets", obj_Bullet);
nnn.damage = 5 + irandom(5);
nnn.speed = 8;
nnn.direction = point_direction(x, y, obj_Player.x, obj_Player.y);

See how we set its variables using the point method outlined in the section on Addressing Variables? This will give the bullet objects a different damage value. But what about the player object? How will it detect the damage that it has to take? By using other in the collision event:

hp -= other.damage;
if hp <= 0 instance_destroy();

The above code will deduct the amount stored in the other instance in the collisions "damage" variable from the player "hp" variable, then it will check to see if the "hp" is lower than or equal to 0. If it is then it will destroy the player object. Please note that other used in this way only works in the collision event and that the other instance must have the variable being checked or else an error will be thrown. However you can assign values to variables, or even create new ones, using other in the collision event too, like this:

other.mana += 10; //add ten to the other instance "mana" variable
other.hit = true; //set the other instance variable "hit" to true, creating it if the variable doesn't already exist


Keyword Description value
all All instances currently active in the room.    -3

This keyword is used to tell GameMaker Studio 2 that a function is to be applied, or to check, all active instances within a room (deactivated instances will not be checked or accessed). You cannot use all to access or set variables in other instances using the point method (see here), but you can use it when calling with(), for example:

with (all)
   {
   speed = 0;
   }

The above code will set the speed of all instances in the room to 0. You can also use all within functions to target or check all instances in the room for example:

inst = instance_position(mouse_x, mouse_y, all);               //Check a point for any active instance in the room

if collision_line(x, y, mouse_x, mouse_y, all, false, true) {} //Check all instances for a collision along a line

mp_grid_add_instances(grid, all, false);                       //Add all instances in the room into a motion planning grid

all is a very useful keyword and can be used in numerous situations within your code and actions, often cutting down on the amount of code you need to write to achieve a desired effect.


Keyword Description value
noone No instance at all.    -4

It may seem odd, but many times while programming your games will you find the need to check if there are no instances found at a location, or in a collision etc... In those cases you would use this keyword to check for nothing, something like this:

if instance_nearest(x, y, obj_enemy) != noone
   {
   //do something as there is an enemy instance near
   }

In this example, the function instance_nearest() will return either noone or the unique ID of the nearest found instance. Basically, any time that you need to check for an instance, you can expect to get either noone or a unique instance ID returned.