Get instance ID of object calling code?[SOLVED]

Discussion in 'Programming' started by EbonHeart, Dec 28, 2016.

  1. EbonHeart

    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.
     
  2. The Reverend

    The Reverend Member

    Joined:
    Sep 8, 2016
    Posts:
    553
    each instances id is stored in a built in instance variable called id:
    var intance_id = id;
     
  3. EbonHeart

    EbonHeart Guest

    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.
     
  4. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,616
    The calling instance's object ID is stored in object_index.
     
  5. EbonHeart

    EbonHeart Guest

    Still didn't work. Unknown object in orbitballtest
     
  6. Aura

    Aura Guest

    Inside a with statement, the keyword other refers to the instance calling the statement.
     
  7. EbonHeart

    EbonHeart Guest

    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.
     
  8. Aura

    Aura Guest

    Local variables declared using the var declaration do not need other to be referred to.
     
  9. EbonHeart

    EbonHeart Guest

    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.
     
  10. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,876
    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.
     
  11. EbonHeart

    EbonHeart Guest

    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.
     
  12. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,876
    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.
     
  13. EbonHeart

    EbonHeart Guest

    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.
     
  14. t0r1x

    t0r1x Guest

    Thanks for raising this question.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice