Get instance ID of object calling code?[SOLVED]

E

EbonHeart

Guest
Exactly what's in the title. Is there a way to get an object's instance ID from within that object? Or do you have to get it as the object is created, then pass it to that object? I have an object in my game that creates other objects that follow it. My code for that only works if there is just one instance of the original object. But I could give the children objects a "target" variable and have them follow target.x and target.y if the parent could just pass them its instance id. I tried just doing id and it didn't work.
 
E

EbonHeart

Guest
each instances id is stored in a built in instance variable called id:
var intance_id = id;
I tried
var inst, i, angleset, radiusset, test;
test = id;
angleset = 0;
i = 0;
for(radiusset = 100; radiusset > 0; radiusset -= 15){
for(i = 0; i < 4; i++){
inst = instance_create(x,y,orbitballtest);
with(inst){
angle = angleset;
target = test;
radius = radiusset;
angleset += 1.57;
}
angleset += .035;
}
}
Where orbitballtest is a little fireball that is programmed to orbit around target. (I used a thing I'd already made for this test) And it didn't recognize the object associated with id.
 
A

Aura

Guest
Inside a with statement, the keyword other refers to the instance calling the statement.
 
E

EbonHeart

Guest
Inside a with statement, the keyword other refers to the instance calling the statement.
That is good info. That being said, how on earth does the rest of that code work? Because the angle = angleset; and radius = radiusset; inside of the with statement work fine.
Also, still doesn't work. here's what I have.

orbittest Create event:
var inst, i, angleset, radiusset, test;
test = object_index;
angleset = 0;
i = 0;
for(radiusset = 100; radiusset > 0; radiusset -= 15){
for(i = 0; i < 4; i++){
inst = instance_create(x,y,orbitballtest);
with(inst){
angle = angleset;
target = other.test;
radius = radiusset;
angleset += 1.57;
}
angleset += .035;
}
}

orbitballtest Create event:
target = 0;
follow = 0;
angle = 0;
radius = 50;
y = target.y+(sin(angle)*50);
x = target.x+(cos(angle)*50);

orbitballtest Step event:
if(follow == 0){
angle += .013;
y = target.y+(sin(angle)*radius);
x = target.x+(cos(angle)*radius);
}
else if(follow == 1){
follow = 2;
speed = 2.2;
direction = 250 + random(40);
}
if(y<5){
follow = 1;
}

It's worth noting that this works fine if I say target = instance_find(orbittest,1);
But then it only works with one instance of orbittest. Which isn't what I am looking for.
 
A

Aura

Guest
Local variables declared using the var declaration do not need other to be referred to.
 
E

EbonHeart

Guest
Local variables declared using the var declaration do not need other to be referred to.
Even without the other it won't recognize it. As far as I can tell it sets the target in orbitballtest to the object index of orbittest but it doesn't know what that object is.
 

TheouAegis

Member
How do you know it is setting it to the object index? What code are you calling that is causing the error message that tells you it is setting it to the object index? Post the actual error message, because nobody here thinks your code here is wrong.

I can almost guarantee you that it is setting it to the actual ID of the object and that you have code elsewhere that is incorrect and is trying to treat that value as an object index because your other code that you weren't showing us is wrong.
 
E

EbonHeart

Guest
How do you know it is setting it to the object index? What code are you calling that is causing the error message that tells you it is setting it to the object index? Post the actual error message, because nobody here thinks your code here is wrong.

I can almost guarantee you that it is setting it to the actual ID of the object and that you have code elsewhere that is incorrect and is trying to treat that value as an object index because your other code that you weren't showing us is wrong.
I posted all of the relevant code above. The error was in the orbitballtest Create event. Give me just a second and I can post the exact error message.
 

TheouAegis

Member
Oh wait, I see what you did wrong. The create event runs first before any variable values are assigned to the object. You need to have orbittest set orbitaltest's position, and not the other way around.
 
E

EbonHeart

Guest
Oh wait, I see what you did wrong. The create event runs first before any variable values are assigned to the object. You need to have orbittest set orbitaltest's position, and not the other way around.
You are correct. I was just in the middle of typing that. :D Also, object_index makes it track the first instance of the object. But if you set test = id; That code works. And remove the position set in the create event.
 
Top