You are not logged in.
Pages: 1
///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
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
Oh, I didnt see that; I guess it should be using point_distance then -
///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
Pages: 1