The instance-creating functions — instance_create_layer and instance_create_depth — are among the first functions that many new GameMaker Studio 2 developers will learn how to use. The importance of adding copies of specified objects into the game world goes without stating. But, there’s more to these backbone functions than what novice game makers may realize.
Did you know?
Returned from either of these functions is an id. This is the unique identifier of the object instance that was just added to the room. Using this read-only integer, we can "talk" directly to a specific copy of an object (e.g. changing the attribute of only one particular zombie enemy in a hoard of zombies).
This function output isn't new to GameMaker Studio 2. Returning instance identifiers started with the obsolete instance_create function from the software's predecessor versions.
This GameMaker Studio 2 Quick Bit will quickly showcase how you can access a specific instance and, by extension, its variables.
Instance referencing and the "with" construction
Many simple, yet powerful, avenues can be explored by understanding how to handle the information the instance-creating functions return to us. Communicating directly with a particular entity via its identifier allows us to understand:
How to destroy a specific instance
How to get a variable’s value from another instance
How to change the value of a variable inside a different object
The code snippet above creates a new instance of an object named obj_bullet using the instance_create_depth function. Knowing that the function returns the id of the newly-created bullet object instance, we can assign it to be the value of local variable inst. So now whenever we call inst, we're calling the particular instance of the bullet.
The "with" construction in GameMaker
Next, using the with construction, the scope of our code changes from the instance that just called the creation function (say, a player object) to the instance that was just created (the bullet). In other words, we're now talking directly to inst.
Note that as soon as our bullet is destroyed, we won't be able to reference it anymore. Attempting to do so would throw an error. So, outside of local scope, it's smart to perform an instance_exists check.
Lastly, the statement following the construction — the code inside the brackets — is executed inside the instance of obj_bullet that was just created, inst.
Special keyword, “other”
The special keyword, other, can be used in two distinct ways. You can read a more thorough explanation in the official YoYo Games documentation entry for the special keyword, other. But, in the context of the code snippet above, other escapes the with code block and refers to inst.
Hopefully you learned something new about the instance-creating functions, instance_create_layer and instance_create_depth in GameMaker Studio 2. To recap, here is what you should take away from this Quick Bit:
Returned from instance-creating functions is a read-only, id integer
The id is assigned to the newly-created instance in the room
You can directly reference an instance using the with constructor
It’s recommended you use instance_exists check to ensure the instance exists before attempting to address it
The special keyword, “other”, when used inside a “with”, refers to the instance calling “with”.
This GameMaker Studio 2 Quick Bit contains information from sources hyperlinked in this article as well as the following: