Jump to content
Sign in to follow this  
TheGeneral2899

Get Players Within X Distance of Object

Recommended Posts

Back for another question!

 

I'm making a form of Radar system for a map I am working.  Heres how it should work:

1) Player(s) walk within 4 meters of object to activate

2) Save the player / players into a variable.

3) Run code on this player WHILE they are within range.

4) Remove the players from code once they move away.

 

Here is what I have so far: 

 

// Mission Timer (5 minutes)
_radarEndTimer = 300;

// Start of master while loop to end when timer runs out
while {_radarEndTimer > 1} do {

	while {{player distance radarStation < 3} count allPlayers > 0} do {
		playersInRange = [];
		{ playersInRange pushBack _x; } forEach nearestObjects [radarStation, [], 3];
		sleep 0.5;

			while { Player is inside the array of players inside the range} do {
				// Run the code on this guy
			}
	};
      
	_radarEndTimer = _radarEndTimer - 1;
	// hint format ["Time left : %1", _radarEndTimer];
	sleep 1;
};

Thanks for any help!

Share this post


Link to post
Share on other sites

Try this:
_nearestPlayer=([allPlayers,[],{radarStation distance _x},"ASCEND"]call BIS_fnc_sortBy)select 0;

 

Gets list of all players, sorts them all nearest-to-farthest from radarStation, then selects the nearest.

  • Like 1

Share this post


Link to post
Share on other sites

Whats the point of sorting them? Way I understood it, any player in range of the radar gets detected/marked (whatever he wants to do).

 

 

Anyway, this is bascially very simple.

Hell you could even do by spawning triggers on those radar stations.

 

_radarEndTime = serverTime + 300;

while { serverTime < _radarEndTime } do {
	{
		private _radar = _x;
		{
			// code run on player in range
		} forEach (allPlayers select { _x distance2D _radar < 3 });
	} forEach radarStations;
	sleep 1;
};

(used an array there, in case you want more than 1 radarStation)

 

 

Share this post


Link to post
Share on other sites

@Tajin - Super interesting concept... Didn't even think of spawning the trigger on top of the radar station and use that.   

 

More importantly though, in regards to the code you pasted above:

1)  Once the player is no longer in range, the code will stop performing non them correct?  It's basic purpose is to illustrate enemy players on map (via markers) once they are inside the range, however once they leave the range, remove the markers / end the code.

2)  Didn't grasp the forEach radarStations part.  I imagine radarStations is an array where I can populate radar1, radar2 etc.  If I just wanted one though (which is the plan)?

 

Appreciate the responses!

Share this post


Link to post
Share on other sites

1) that largely depends on how that code you want to run looks. You could also remove the "select" bit and instead use an if/else block that checks the distance of each player. That way you can also run code on the ones who're not in range.


2) just put one in the array then ^^ (or remove that forEach loop and set _radar to the one you want)

 

 

 

Share this post


Link to post
Share on other sites

Right.  I think the basic structure of it (which was the key here) is where I am struggling.  

 

I need to be able to catch once a player has left the area and stop running a script (i.e. delete the markers of player locations).

 

If I were to use an If/Else block as you suggested, I could basically say if player is within the range of the radar, run function that shows players, else remove them ya?

 

I'm going to set it on it a bit more tonight and will update here.  

Share this post


Link to post
Share on other sites

 

Here, try it like this:

_radarEndTime = serverTime + 300;

while { serverTime < _radarEndTime } do {
    {
        private _radar = _x;
        {
            _marker = format["p_%1", getPlayerUID _x];
            if (_x distance2D _radar < 3) then {
                if ("" isEqualTo getMarkerType _marker) then {
                    _marker = createMarker [_marker, _x];
                    _markerstr setMarkerShape "ICON";
                    _markerstr setMarkerType "hd_dot";
                };
                _marker setMarkerPos getPosATL _x;
            } else {
                deleteMarker _marker;
            };
        } forEach allPlayers;
    } forEach radarStations;
    sleep 1;
};

 

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  

×