• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Help me understand this code that works

inertias

Member
So someone asked a question on reddit about instances following the individual object that creates them.

If you look at the posted solution, it works perfectly but I don't understand why. Here's what they wrote:

When you create the object, give it a variable that stores the id of the instance that created it.
Code:
var inst = instance_create(x,y,obj);
inst.creator = id;
Now, since the newly created instance of that object has that variable, you can use it to snap to that position.
Code:
x = creator.x;
y = creator.y;
Haven't been programming for long so I don't understand the "inst.creator" part. What exactly is happening there? What is "creator" in this case? I've only seen the use of a "." to reference a variable from an object. For example, obj_player.x will get the player's x coordinate. So what is inst.creator doing?

Can anyone either explain or link me to the doc's explanation? Thank you! :)
 

samspade

Member
The function instance_create not only creates an instance of an object, it returns the specific instance id of that object. In this case:

Code:
var inst = instance_create(x, y, obj);
saves that instance id to a temporary variable named 'inst.'

If you have a specific instance id you can access and change the variables inside of that specific instance in one of two ways. Both of the following are valid:

Code:
inst.creator = id;

with (inst) {
    creator = other.id;
}
In the first case you are access the variable from the current object so you don't need the keyword other. In the second case, it is as if the code is running from inside of the other object so creator doesn't need any 'prefix' and id needs the keyword other. Inside of a with statement the keyword other contains the instance id of the calling object. So in the second example, the keyword other is doing the exact same thing as inst is in the first example.

What inst.creator = id; does, therefore is save the instance id of the instance running the code to that object so it can be used later on.

The second portion of code is doing the same thing. It is using the variable creator, which contains the instance id of the original instance to access that instance's x and y variable and set its x and y to the other instance's x and y.

This concept is very, very useful and is used all of the time for a bunch of different things. For example, this same idea can be used to prevent bullets from harming the instance that created them.

These articles might be helpful to read as well:

https://docs.yoyogames.com/source/d..._gml language overview/401_05_addressing.html

https://forum.yoyogames.com/index.php?threads/whats-the-difference-objects-and-instances.29005/
 
Last edited:
Top