You are currently viewing the GMLscripts.com static mirror. Forum access and script submissions are not available through this mirror.

Invert GMLscripts.com

instance_nth_nearest

GM:Studio can already tell you the instance nearest to a point, but what about the 2nd nearest? Or the 3rd nearest? Or the nth nearest? This script answers that question.

Download
instance_nth_nearest(x,y,obj,n)
Returns the id of the nth nearest instance of an object to a given point or noone if none is found.
COPY/// instance_nth_nearest(x,y,obj,n)
//
//  Returns the id of the nth nearest instance of an object
//  to a given point or noone if none is found.
//
//      x,y       point coordinates, real
//      obj       object index (or all), real
//      n         proximity, real
//
/// GMLscripts.com/license
{
    var pointx,pointy,object,n,list,nearest;
    pointx = argument0;
    pointy = argument1;
    object = argument2;
    n = argument3;
    n = min(max(1,n),instance_number(object));
    list = ds_priority_create();
    nearest = noone;
    with (object) ds_priority_add(list,id,distance_to_point(pointx,pointy));
    repeat (n) nearest = ds_priority_delete_min(list);
    ds_priority_destroy(list);
    return nearest;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw