Jump to content
Sign in to follow this  
meatball

Function to find closest player to target

Recommended Posts

I'm working on a function I can repeatedly call that can check to see which player is the closest to a target. Here's what I have so far and haven't been able to get it working:

mb_fnc_findNearest = {
private ["_target","_nearestUnit"];
_target = _this select 0;
_nearestUnitTemp = playableunits call BIS_fnc_selectRandom;
_nearestUnitDist = _nearestUnitTemp distance _target;
{if (_target distance _x < _nearestUnitDist) then {_nearestUnit = _x;_nearestUnitDist = _target distance _x};
} foreach playableunits;
_nearestUnit;
};

Anyone have any idea what I'm screwing up here? This function will run pretty often, so any ideas where it can be more efficient would be appreciated as well. Thanks in advance!

Edited by Meatball

Share this post


Link to post
Share on other sites

I'm on my lunch break so can't test, but perhaps something like this:

mb_fnc_findNearest = 
{
private ["_target","_nearestUnit","_players","_sorted"];
_target = _this select 0;
_players = [];

// Get array of actual players
	{
	if (isplayer _x) then
		{
		_players set [count _players,_x];
		};
	} foreach playableunits;

// Sort
_sorted = [_players,[],{_x distance _target},"ASCEND"] call BIS_fnc_sortBy;
_nearestunit = _sorted select 0;
_nearestunit;
};

Share this post


Link to post
Share on other sites

Something I use frequently (Very similar to TPW's method) however for my purposes it ignores players in air vehicles.

_p = _this select 0;
_d = objNull;
_r = 10000;
{
   if(!((vehicle _x) isKindOf "AIR")) then
   {
       _n = _x distance _p;
       if((isPlayer _x) && (_n < _r)) then
       {
           _d = _x;
           _r = _n;
       };
   };
} forEach playableUnits;
_d

Share this post


Link to post
Share on other sites

Ah, nice, they both look great and I'll give them a shot. Which do you think would be less resource intensive?

Share this post


Link to post
Share on other sites

Kushluk's will chew slightly less CPU because it's not actually sorting based on distance. I have no idea how efficient BIS_fnc_sortBy is.

But in reality, if you call the function every 10 seconds or something like that, it will make bugger all difference.

Share this post


Link to post
Share on other sites

Ah, excellent. Followup question then, part of the function will check the distance and depending on the distance, the target will attempt to run away. Setting up waypoints to a point in a direction away from the player isn't too hard, but I'm limited to Limited, Normal and Full speeds (seems to be no real difference between normal/full to me). I would like the target to try to sprint away as well, but I can't figure out how to coordinate a move command with the sprint animation (Animation "AmovPercMevaSnonWnonDf").

Any idea how I can force the unit to actually sprint from point A to point B?

Share this post


Link to post
Share on other sites

Try using setvelocity in combination with setdir and switchmove "AmovPercMevaSnonWnonDf". Just a thought, I've never actually used it.

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
Sign in to follow this  

×