TurtleYawns
Member
I am struggling to find a working code that returns the nth nearest of a specific object.
I want to create a piece of code for obj_predator to hunt for prey that fit its requirements, the loop will check the nearest of obj_animal and then see if its variable values match the requirements for the obj_predator if they don't the loop will check the next nearest and keep repeating until it finds a valid target. I have the following code that is from GMS but it doesn't work for GMS2.
This is put into a script and the nth nearest id is returned
I want to create a piece of code for obj_predator to hunt for prey that fit its requirements, the loop will check the nearest of obj_animal and then see if its variable values match the requirements for the obj_predator if they don't the loop will check the next nearest and keep repeating until it finds a valid target. I have the following code that is from GMS but it doesn't work for GMS2.
This is put into a script and the nth nearest id is returned
-
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; [*]
Last edited: