• 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!

GML Visual Connecting two instances

Right now I have a bar spawning at the top of an enemy that follows it. Here is my problem, when I create a second of the same instance the bars only follow the first one instead of each new one having its own bar. Is there a way for an instance to know which instance created it and only follow that one?

I am using dnd and gms2.

Thank you!
 

Slyddar

Member
What you need to do is record the instance id of the bar that each enemy has associated with it, say in a variable called bar_id. Then in the enemy code that moves the bar, ensure it only runs if the id of the bar is bar_id. That way you can limit the movement to only happen to it's own bar.

When you create the bar instance, you can use the target section to set a variable to the id of the instance created.

Like this, assuming you are creating the bar from within the enemy object.
23-07-2019 11-47-37 AM.png
 
Last edited:
Thanks but I am still a little confused. If I am writing the variable in the target section won't each bar end up with the same id as they are spawned?
 
What you need to do is record the instance id of the bar that each enemy has associated with it, say in a variable called bar_id. Then in the enemy code that moves the bar, ensure it only runs if the id of the bar is bar_id. That way you can limit the movement to only happen to it's own bar.

When you create the bar instance, you can use the target section to set a variable to the id of the instance created.

Like this, assuming you are creating the bar from within the enemy object.
View attachment 25868
Ok I think I am starting to get what you are saying. Sorry I am a little slow on understanding all of this stuff. How would I get the instance id to store in a variable?
 

FrostyCat

Redemption Seeker
Ok I think I am starting to get what you are saying. Sorry I am a little slow on understanding all of this stuff. How would I get the instance id to store in a variable?
That's what the "Target" field is for, the variable name to store the instance ID into goes there. TheSly has it set to bar_id. When you update the bar's coordinates from the enemy's Step event, use the Jump to Point action with its Apply to: expression set to bar_id and the coordinates set to other.x and other.y.
 
Thank you! That makes sense and I got it working! Is there now a way for the bar to know the instance id of the enemy that created it?
 

FrostyCat

Redemption Seeker
Thank you! That makes sense and I got it working! Is there now a way for the bar to know the instance id of the enemy that created it?
Right after the Create Instance action that TheSly showed you, use the Assign Variable action to set bar_id.owner_id to id. Then the originating instance ID is accessible anywhere within the created obj_bar instance except for its Create event. It's a trivial variation of synchronizing coordinates.
 
So once this is set, if I want something touching the bar object to effect the synced object would I just use object_enemy and it would know which one?
 

FrostyCat

Redemption Seeker
You must learn the difference between an object and an instance. Whenever "which one?" becomes an open question because there are multiple instances of the object, you can't reference off an object ID like obj_enemy anymore, you must reference off a specific instance ID. You cannot meaningfully talk about "human's hair colour" in a room full of people, you can only talk about the hair colour of specific individuals (e.g. "Alice's hair colour").

Finding the correct instance ID in a collision is covered in the linked article:
Collision events (the ones from the event selector, NOT collision checks in the Step event): other refers to the colliding instance in this event only. IMPORTANT NOTE: other is always equal to -2 and will lose context outside the event. If you need the actual instance ID later, you need to use other.id instead.

Collision-checking functions: instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ all return instance IDs. Save that instance ID into a variable (e.g. var inst_enemy = instance_place(x, y, obj_enemy);), then use that as the subject to work with (e.g. inst_enemy.hp -= 10;). Note that these functions return noone upon not finding a collision. Always account for this special case whenever you handle collisions this way.
Assuming that owner_id is set as described in post #7, a collision event with obj_bar would use other.owner_id to refer to the bar's owner, not obj_enemy. Accessing the owner's variables would be done like this:
Code:
other.owner_id.hp
Not like this:
Code:
obj_enemy.hp
This isn't difficult to understand, you just need to start letting things other than object IDs sit on the left side of a variable reference dot.
 
Top