• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Nearest Instance by x only

TheouAegis

Member
Code:
var dist = room_width, target=-4;
with argument0 if abs(x-other.x) < dist
{
    dist = abs(x-other.x);
    target = id;
}
return target;

Put that code in a script, pass the object or parent you want to look for. It will return either -4 if no target is found or the id of the target.
 

jo-thijs

Member
You can do it much more optimized though:
Code:
///scr_instance_nearest_x(x, obj)

var result = noone;
var dx = -1;

with argument1
    if result == noone || abs(x - argument0) < dx {
        result = id;
        dx = abs(x - argument0);
    }

return result;
EDIT:
Ninja'd by a more performant, but less general code.
 
Top