Hi,

I want to calculate the closest entity given in a scan range each time to a target point, please see the image:




1 - Entity 1 has a scan range using SCAN_ENTITY;

2 - Inside that scan range the event_detec its used to enumerate the scanned entities and saved the handle;

Code:


if(event_type == event_detect)
{
TEMP_SCANNED_ENTITY = you;
ds_scan_array_list[ds_scanEntityID] = you;
ds_scan_array_list[ds_scanEntityID] = handle(TEMP_SCANNED_ENTITY);
ds_scanEntityID += 1;





3 - Get the closest detected entity to the target point every time;

Code:


function getClosestEntity(&obj) //obj will be target point
{
var iDist;
var i;
you = 0;
my = obj;
while(i < ds_scanEntityID)
{
if(ds_scan_array_list[i] == 0)
{
i += 1;
continue;
}
ds_TEMP_CLOSEST_ENTITY = ptr_for_handle(ds_scan_array_list[i]);
i += 1;
if(!ds_TEMP_CLOSEST_ENTITY)
{
continue;
}
if(you == 0 || vec_dist(ds_TEMP_CLOSEST_ENTITY.x,my.x) < iDist)
{
you = ds_TEMP_CLOSEST_ENTITY;
iDist = vec_dist(you.x,my.x);
}
}
ds_SCANNED_CLOSEST_ENTITY = you;
return (iDist);
}




4 - So, ds_SCANNED_CLOSEST_ENTITY should be always the closest detected entity to the target point, but sometimes it gives strange results. Sometimes it returns the closest entity and sometimes dont work it gives another entity.

What´s the problem in here?

5 - One last question: how can i implement the code in order to enumerate the closest entities to the target point by descendant order?

Thanks in advance.