Legacy GM Refer the Instance ID of the object calling code?[Solved]

B

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!
 

Simon Gust

Member
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;
}
 
B

Brian Le

Guest
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;
}
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
 

Jezla

Member
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
     }
 
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.
 
B

Brian Le

Guest
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
     }
Thanks all of you guys, now I understand more about that!
 
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 (?)
 
Top