• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code instance_nearest not self

Imperial

Member
I believe the only missing argument of instance_nearest function is the notme argument

Like the collision_circle...It has the notme to avoid the object from detecting It self

I tried this Scripts but It doesn't work as It should be
Code:
/// 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;
}
and this one
Code:
var xx,yy,obj,nearest;
xx = argument0;
yy = argument1;
obj = argument2;
nearest = noone;
dist = -1;

for(i = 0; i < instance_number(obj); i++)
{
    var o,d;
    o = instance_find(obj,i);
    d = point_distance(xx,yy,o.x,o.y);
    if(o != id)
    {
        if(nearest == noone || d < dist)
        {
            nearest = o;
            dist = d;
        }
    }
}
YoyoGames, can you please add the notme argument to the instance_nearest function please
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
This is something that YYG are looking at and there is a bug filed for the instance_nearest/furthest functions to get an (optional) "notme" argument added to them.

As for a workaround, the simplest one is to do this:

Code:
// scr_instance_nearest_notme;
var _x = x;
x -= 10000000;
var _inst = instance_nearest(_x, y, OBJECT);
x = _x;
if _inst != id && _inst != noone
{
// You found an instance!
return _inst;
}
return noone;
 

Franky93

Member
I was thinking how to solve this problem, and this idea came to my mind:

Code:
create event:
distance=max(room_width,room_height)
step event:
with (object) if id != other.id {if distance_to_object (other) <other.distance other.distance = distance_to_object (other)}
with (object) if id != other.id {if distance_to_object (other) = other.distance {code}}
distance = max (room_width, room_height)
Here is the code how you can find the closest Object instance
 
Last edited:
@Happy_end_end please use correct formatting (all code should be between[code][/code] tags), the example you've given is pretty unreadable. Also, what's wrong with the standard solution that nocturne provided? It doesn't require instance variables, it's a lot more compact and can be run on any instance without requiring a control object.
 

Franky93

Member
RefresherTowel


I'm not saying that his code is worse, just as an idea of how to find the closest instance, by the way, it can also be launched in any instance, but you are right about the control object
 
Top