To make certain things easier in GameMaker: Studio, 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, so care must be taken when
assigning values to variables, as you may get unexpected results
later as GameMaker: Studio 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 interprets as the unique ID for the
instance. In general you should never need to use this keyword, as
you can do the same task more efficiently and appropriately using
other keywords or functions, and is maintained for legacy support
reasons.
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 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(x, y, 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 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 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.