Legacy GM instance following the 'correct' instance

T

TTJ

Guest
Hi,

I've been wondering how to do this.

I got a gun which essentially is the player and a body that follows the player in the end step.
Code:
if instance_exists(obj_Player){
        x = obj_Player.x;
        y = obj_Player.y;
    }
Which works fine with only one player. But i got four. I initially thought i could store the instance_id in the gun and pass it onto the body when it is created inside of the gun. Then in the step event i could follow only the instance which has the correct ID. But it seems i can't use instance_id in that manner.

Any thoughts?
 
B

bojack29

Guest
Um. Your thinking is exactly right. You must have done something wrong.

Place the id of the parent within the child object at creation, then with the child object you merely assign itself to the parents coordinates
 
T

TTJ

Guest
Well that's a first :) Seems i'm on the right track. Unsure on the execution though.

Code:
if instance_exists(obj_Player){
        if obj_Player.instance_id = followID {
            x = obj_Player.x;
            y = obj_Player.y;
        }
    }
Is this it? If so, there must be another reason why my player2 sprite isn't showing up
 
B

bojack29

Guest
give the child object your id
Code:
//Parent create
with(instance_create(x, y, obj_child)){
     parent_id = other.id;
}

//Child step
x = parent_id.x;
y = parent_id.y;
 
T

TTJ

Guest
Ah, think it didn't worked for me because i wanted to store the variable of the parent id before passing it over. I also used instance_id instead of just id.

Also didn't know you could create an object with, with. I always used this method:
Code:
new = instance_create (x,y,object)
new.parent_id = id.
Anyway, your method worked. Thanks!

Any advice to spare on how to prevent adding if instance_exists in every object to prevent crashing when it is destroyed?
 
B

bojack29

Guest
You can set a flag in the child that tells it that the parent is no longer available. Specifically in the parent's destroy event.
 
Top