GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2017-08-17 17:07:00

r0wan
Member
Registered: 2017-08-17
Posts: 3

instance_nearest_multiple

Expand///instance_nearest_multiple(x,y,object1,object2,etc...);
//
//gets the nearest instance out of multiple types of objects
//
//
//      x      the x position to check from
//      y      the y position to check from
//      object1 - etc...       object type to check for
//
/// GMLscripts.com/license

var _x = argument[0];
var _y = argument[1];
var r = instance_nearest(_x, _y, argument[2]);
var rd = distance_to_object(r);
for (var i = 3; i < argument_count; i++) {
    var q = instance_nearest(_x, _y, argument[i]);
    var qd = distance_to_object(q);
    if (qd < rd) { rd = qd; r = q; }
}
return r;

Offline

#2 2017-08-18 13:31:18

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: instance_nearest_multiple

Thank you, r0wan, and welcome to the forums.

I think there is a potential logic problem here. You are looking for the instances nearest a given point but you are measuring the distance of each from the calling instance. In practice these are unlikely to be the same thing.

(mouse_x,mouse_y)      A3      B6      C2      ID:instance_nearest_multiple(mouse_x, mouse_y, A, B, C)

Of objects A, B, and C, A3 is the closest to (mouse_x,mouse_y) but C2 is the closest to the calling instance (ID). Even if the calling instance passed its own (x,y) coordinates to the script, the distances measured would probably not be the same because distance_to_object() measures the distance between the bounding boxes of two instances, while instance_nearest() measures the distance between a point and an instance origin.


Abusing forum power since 1986.

Offline

#3 2017-08-26 08:53:57

r0wan
Member
Registered: 2017-08-17
Posts: 3

Re: instance_nearest_multiple

Oh, I didnt see that; I guess it should be using point_distance then -

Expand///instance_nearest_multiple(x,y,object1,object2,etc...);
//
//gets the nearest instance out of multiple types of objects
//
//
//      x      the x position to check from
//      y      the y position to check from
//      object1 - etc...       object type to check for
//
/// GMLscripts.com/license

var _x = argument[0];
var _y = argument[1];
var r = instance_nearest(_x, _y, argument[2]);
var rd = point_distance(_x,_y,r.x,r.y);
for (var i = 3; i < argument_count; i++) {
    var q = instance_nearest(_x, _y, argument[i]);
    var qd = point_distance(_x,_y,q.x,q.y);
    if (qd < rd) { rd = qd; r = q; }
}
return r;

Offline

Board footer

Powered by FluxBB