Jump to content
ditrapanij

Get closest player to marker

Recommended Posts

Hi All,

 

Looking for assistance with how to find the closest player to a marker. Have tried using nearestObject with a type of "Player", this returned a Nil result.

 

Any help is greatly appreciated.

 

Regards,

Share this post


Link to post
Share on other sites

"player" is not a type/classname.

 

private _markerPos = getMarkerPos "markername";
private _playerList = allPlayers apply {[_markerPos distanceSqr _x, _x]};
_playerList sort true;
private _closestPlayer = (_playerList select 0) param [1, objNull];

 

Edited by Dedmen
Use distanceSqr instead as it's faster
  • Like 3

Share this post


Link to post
Share on other sites

And if you don't need the distance value, use distanceSqr instead of distance to make the script faster.

  • Like 2

Share this post


Link to post
Share on other sites
57 minutes ago, atmo said:

Does BIS_fnc_nearestPostiion do the job?

 

https://community.bistudio.com/wiki/BIS_fnc_nearestPosition

 

Only one pass through the data.. no sorting...etc

 

Sure! But it takes much longer time to run.

Just make a test in console, like:

[allUnits,trigger1] call Bis_fnc_nearestPosition;

 

vs what dedmen wrote:

someList = allUnits apply {[trigger1 distanceSqr _x, _x]};
someList sort true;
closestUnit = (someList select 0) param [1, objNull];

 

With 20 or 30 units... You see the difference?

Share this post


Link to post
Share on other sites

OK, for a small amount of units it is 'efficient' but....code wise you first go through the someList array once with the 'apply' that's O(n), then you sort with what I presume is a quicksort which on average works at O(n log n), and in the worst case scenario at O(n^2)… whereas BIS_fnc_nearestPosition goes through the list once O(n)..

 

It may be faster with sorting and then pinching the first element for small numbers (in the tens maybe) but I'm not so sure about larger arrays.... (I will go and test this for fun) 

 

but I think the issue really is cleaner code? 

 

[allUnits,trigger1] call Bis_fnc_nearestPosition;

 

one line, can accept objects/positions/markers.

 

Shouldn't we encourage safe, clean code which will always work? Some day we may have 1000 people on a server and if  'legacy code' is not going to cripple a function in the future we should be wary maybe? The 'worst case scenario' has an O(n^2) efficiency in it. but I am probably picking hairs..An interesting discussion.

 

Atmo 🙂

 

 

 

Share this post


Link to post
Share on other sites

Nah ok...

 

Result:
2.275 ms

Cycles:
440/10000

Code:
someList = allUnits apply {[trigger1 distanceSqr _x, _x]};  someList sort true; closestUnit = (someList select 0) param [1, objNull];


Result:
15.2576 ms

Cycles:
66/10000

Code:
[allUnits, trigger1] call BIS_fnc_nearestPosition;

 

 for about 1500 units....

 

😞

 

Share this post


Link to post
Share on other sites

usually solutions wirh engine command are faster as bis function.

but u did it the right way. you tested both and got a result. that is what counts!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×