GameMaker How do I refer to an object using it's id?

L

Levi Davis

Guest
In my game you place down a few points and hit go, and when you hit go the character appears at point one and is supposed to move from point 1 to point 2 to point 3 etc, so I'm using instance_find() to find the id of the next point, but I'm unsure how to then save that id and use it later on to find the x and y of the object that has that id. Any ideas?
 
Last edited by a moderator:

rIKmAN

Member
In my game you place down a few points and hit go, and when you hit go the character appears at point one and is supposed to move from point 1 to point 2 to point 3 etc, so I'm using instance_find() to find the id of the next point, but I'm unsure how to then save that id and use it later on to find the x and y of the object that has that id. Any ideas?
instance_find() returns the instance id of the found instance, or noone if none is found.

Store the return value from the instance_find() function and then you can use it later and retrieve it's x/y position.
Make sure you check the variable holding the return value actually contains an id before trying to use it, in case it has returned noone.

If it's a preset series of points and you only need x/y values of each point then you could also just use a path.
 
Last edited:

GMWolf

aka fel666
The instance if can be used with the dot notation like so:
Code:
var inst = <some id>;
var xCoord = inst.x;
var yCoord = inst.y;
Where <some id> is a function / variable that returns an instance id, like instance_find
inst will hold the instance id.
inst.x returns the 'x' variable of the object with id 'inst'.
 

JackTurbo

Member
Might be a good idea to create a list or array (maybe a queue) and add the instance IDs to that as they're created.

Then the player objects can work its way through the list rather than relying on separate objects and instance find.
 

YoSniper

Member
I highly recommend storing the ID of a created instance in a variable if you are going to reference it later. Remember that instance_create returns the ID of the newly created instance. Might save you a step so you don't have to use instance_find at all!
 
Top