This function will check all the instances of the given object to see which is nearest to the given point of origin. If no instances of the object exist, the function will return the keyword noone, but if there are instances then it will return the id of the instance found. Please note that if the instance running the code was created as an instance of the object being checked, then it will be included in the check.
instance_nearest(x, y, obj);
Argument Description x The x position to check from. y The y position to check from. obj The object to check for instances of.
Real (either an instance ID value or the keyword noone)
var inst, xx;
xx = x;
x -= 10000;
inst = instance_nearest(xx, y, object_index);
if inst != id
draw_line(x, y, inst.x, inst.y);
x += 10000;
The above code move the current instance 10000 pixels then check its previous position to find the nearest instance of the same object type. If that instance is itself, it will do nothing more than move back to its original position, but should the instance found be different, it will draw a line between the two.