Advertisement
gmlscripts

instance_nth_nearest

Oct 3rd, 2023
1,757
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Game Maker 0.78 KB | Source Code | 1 0
  1. /// @func   instance_nth_nearest(x, y, obj, n)
  2. ///
  3. /// @desc   Returns the nth nearest object instance to a given point.
  4. ///         If none is found, noone is returned.
  5. ///
  6. /// @param  {real}      x           x-coordinate of point
  7. /// @param  {real}      y           y-coordinate of point
  8. /// @param  {object}    obj         object index (or all)
  9. /// @param  {real}      n           proximity
  10. ///
  11. /// @return {instance}  object instance found (or noone)
  12. ///
  13. /// GMLscripts.com/license
  14.  
  15. function instance_nth_nearest(x, y, obj, n)
  16. {
  17.     n = clamp(n, 1, instance_number(obj));
  18.     var list = ds_priority_create();
  19.     var nearest = noone;
  20.     with (obj) ds_priority_add(list, id, distance_to_point(x, y));
  21.     repeat (n) nearest = ds_priority_delete_min(list);
  22.     ds_priority_destroy(list);
  23.     return nearest;
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement