SOLVED How to find an instances coordinates?


Friendly Tyrant
Forum Staff
Well, to find the closest instance you can use the GML function instance_nearest(), and to get the coordinates, you can use a "with" call, like this:

var _array = array_create();
var _num = 0;
with (OBJECT)
_array[_num] = x;
_arry[_num + 1] = y;
_num += 2;
This will now have an array in the "_array" variable, where each set of two values is the x/y position for an instance. This is easily expandable to include the instance ID too or any other information you require.

Oh, and I'm assuming that the three instances you mention are all instances of the same object. If they are NOT then let me know, as things will need to be done differently.


It sounds like you might only need the X,Y of the nearest instance of that object?

If that is the case then you could just use:

var target_x = instance_nearest(x, y, obj_enemy).x;
var target_y = instance_nearest(x, y, obj_enemy).y;
Called from within the player object's code.

target_x, target_y will be the coordinates of the nearest instance of "obj_enemy" to the player

or if you only want to know the id of the nearest instance of obj_enemy:

var target_enemy = instance_nearest(x, y, obj_enemy);
Then target_enemy will store the id of the nearest instance of obj_enemy

But yeah, if you do want to store all of the coordinates of each instance of obj_enemy then listen to Nocturne.

(note that I've used local variables so they wouldn't be stored for longer than the script / event runs. You can use non-local variables if you need them stored pst the end of the script)
Last edited: