Game Maker Community: Instance_closest, And Second Closest - Game Maker Community

Jump to content

Advanced Users Forum Rules

This is a Q&A forum for advanced GML users ONLY. Moderators will remove inappropriate topics. Make sure that you READ these rules prior to posting: Advanced Users Forum Rules
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Instance_closest, And Second Closest find the instances after the nearest...

#1 User is offline   PL productions 

  • GMC Member
  • Group: GMC Member
  • Posts: 62
  • Joined: 27-June 05

Post icon  Posted 12 May 2006 - 05:59 PM

here's something that would help me and maybe others a lot if it could be done simply enough. how would you go about finding the second, and third, instance closest, like the instance_nearest function, but how would i find the one that are next? with variables maybe? indexes? arrays? i'm completely clueless on this subject...
0

#2 User is offline   KaizuSellgren 

  • Tic Tac Toe Master
  • Group: GMC Member
  • Posts: 184
  • Joined: 07-September 05

Posted 12 May 2006 - 07:05 PM

There is a script for that. I'm now going to post one. There are many these kind of scripts so I'm not so sure is this like someone's... anyway, here's my fixed version, may look same than some other, but anyway...

// instance_nearest_nth(x,y,obj,nth)
// If n = 1, then it returns the object himself
var x1,y1,n,pq,a;
x1 = argument0;
y1 = argument1;
n = argument3-1;
pq = ds_priority_create();
with (argument2)
 ds_priority_add(local.pq,id,sqr(x-local.x1)
+sqr(y-local.y1)); // This can be done with point_distance also

repeat (n)
 ds_priority_delete_min(pq);

a = ds_priority_find_min(pq);
ds_priority_destroy(pq);

return a;


NOTE: This required GM 6.x Registered...

This post has been edited by KaizuSellgren: 12 May 2006 - 07:07 PM

0

#3 User is offline   §-«»-§ 

  • GMC Member
  • Group: GMC Member
  • Posts: 83
  • Joined: 25-April 06

Posted 12 May 2006 - 07:14 PM

Well, the (very inefficent) way I'd go about doing it is putting all objects in level into a grid and then using a for loop + the useful circle and square search functions there to find them. But that could be very taxing on old computers.
0

#4 User is offline   PL productions 

  • GMC Member
  • Group: GMC Member
  • Posts: 62
  • Joined: 27-June 05

Posted 12 May 2006 - 07:14 PM

Thank you. It works beautifully! This will come in handy!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users