GM:S 1.4 Refer the Instance ID of the object calling code?[Solved]

Discussion in 'Programming' started by Brian Le, Jul 8, 2017.

  1. Brian Le

    Brian Le Guest

    Good morning, afternoon, evening wherever you are, today, I want to ask you something pretty simple to all of you, about Instance ID. I have known that there are 2 built-in variables for referring the Instance ID, which are object_index and id. However, after creating a variable and store the value of the Calling Object, it still didn't work. Here is my code
    Obj_Police Create Event:
    Code:
    var self_id = id;
    var inst = instance_create(x,y,obj_police_gun);
    
    
    Obj_Police Step Event
    Code:
    with (inst) {
            x = self_id.x-2;
            y = self_id.y-2;
    }
    
    The above code checks the id of the police gun and the police, and stick them together base on the ID.
    However, it didn't work although no errors were found.
    Please help me with that, thanks a lot!
     
    Martin Znamenáček likes this.
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,244
    This is a scope issue. You're telling obj_police to forget what self_id is after creation.
    Try
    Code:
    self_id = id;
    var inst = instance_create(x,y,obj_police_gun);
    
    Code:
    with (inst) {
           x = self_id.x-2;
           y = self_id.y-2;
    }
    
    Other than that, you don't have to use self_id.
    You can use "other":
    Code:
    var inst = instance_create(x,y,obj_police_gun);
    
    Code:
    with (inst) {
           x = other.x-2;
           y = other.y-2;
    }
    
     
  3. Brian Le

    Brian Le Guest

    The first solution didn't work for me, however the second does. I think the first solution you just change the line var self_id into self_id?
    Anyways, thanks for that
     
  4. Jezla

    Jezla Member

    Joined:
    Jun 20, 2016
    Posts:
    433
    EDIT: Ninja'd! But I missed the scoping issue.

    Firstly, id and object_index are not the same thing. Id refers to the instance id of the calling instance, but object_index refers to the resource id of the object from which the instance is created (an object is a template, an instance is a copy of an object that is placed within the game room).

    Secondly, to refer to the calling instance's id, simply using id is enough, though if you are referring to it's own variables or built-in variables, simply use the variable name.

    However, what's causing your problem is that your trying to do this inside a with statement. In a with statement, you're essentially asking the instance inside the brackets to run the code (in this case, inst). There's a special keyword to refer back to the calling instance in this case, other. Your code should be:

    Code:
    with (inst)
         {
              x = other.x-2;
              y = other.y-2
         }
     
  5. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,352
    var inst .....
    in the create event won't work either, for the same reason.

    Needs to be just

    inst = ....


    Then in step event

    inst.x = x-2
    inst.y = y-2

    Will also work.
     
  6. Brian Le

    Brian Le Guest

    Thanks all of you guys, now I understand more about that!
     
  7. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    705
    I don't think anyone covered this - I know that Indianabones pointed out it was wrong. As they stated you don't want VAR for a variable you must access later - this is the reason why:

    By using VAR to define the variable, you're making it local - its only present for the event that runs it, in this case the create event only. The variable is not there to be accessed later, as its not held in memory beyond a temporary scope, and so anything reliant on it after the create event should cause an error.

    My own project will crash when a variable hasn't been specified properly, so I'm unsure why yours hasn't crashed - despite it not working. It would seem something that should throw an error, as when checking for 'Inst' after the create event it should have disappeared (?)
     

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