Jump to content
Sign in to follow this  
Sexacutioner

Easier way to count # of player units per side in trigger?

Recommended Posts

So I'm converting a bunch of my OFP scripts to Arma II and I was wondering if there are any new tricks someone knows of to check a trigger area for the number of (players only) units per side present. I used to have to do it with a mess of code like this:

	_triggerArray=list currentCaptureTrigger;
	_totalPresent=count _triggerArray;

	_numResPresent=0;
	_numEastPresent=0;
	_numWestPresent=0;
	_countList=0;
	_maxHeight=captureHeightArray select currentCaptureNumber;

	while "_countList < _totalPresent" do {
		_currentUnit=_triggerArray select _countList;
		_currentUnitCrew=crew _currentUnit;		
		if (((getpos _currentUnit) select 2)<=_maxHeight) then {
			if (east1 in _currentUnitCrew) then {_numEastPresent=_numEastPresent+1};
			if (east2 in _currentUnitCrew) then {_numEastPresent=_numEastPresent+1};
			if (east3 in _currentUnitCrew) then {_numEastPresent=_numEastPresent+1};
			if (east4 in _currentUnitCrew) then {_numEastPresent=_numEastPresent+1};
			if (east5 in _currentUnitCrew) then {_numEastPresent=_numEastPresent+1};

			if (res1 in _currentUnitCrew) then {_numResPresent=_numResPresent+1};
			if (res2 in _currentUnitCrew) then {_numResPresent=_numResPresent+1};
			if (res3 in _currentUnitCrew) then {_numResPresent=_numResPresent+1};
			if (res4 in _currentUnitCrew) then {_numResPresent=_numResPresent+1};
			if (res5 in _currentUnitCrew) then {_numResPresent=_numResPresent+1};

			if (west1 in _currentUnitCrew) then {_numWestPresent=_numWestPresent+1};
			if (west2 in _currentUnitCrew) then {_numWestPresent=_numWestPresent+1};
			if (west3 in _currentUnitCrew) then {_numWestPresent=_numWestPresent+1};
			if (west4 in _currentUnitCrew) then {_numWestPresent=_numWestPresent+1};
			if (west5 in _currentUnitCrew) then {_numWestPresent=_numWestPresent+1};
		};			
		_countList=_countList+1;
	};

That loop contains all units from each team that are assigned names in the editor. It's the only way I found to get it to count actual players (not AI) and count crew in vehicle separate (not a vehicle as only 1 unit).

Are there any fancy new scripting commands since OFP that make this task a bit easier, like just returning #players east?

Share this post


Link to post
Share on other sites

Did not check it, but it just jumped up to my mind:

How about this

{_x == player; side _x == east} count allUnits;

Share this post


Link to post
Share on other sites

Well there is a command called countSide, with an example just showing you what you want - except the fact that it counts all units, including AIs.

Another way was showed by Silderoy, although not entirely correct (_x == player) => (isPlayer _x).

I'd do it like this:

_allUnits = list myTrigger;
_sides = [west, east, resistance];
for "_i" from 0 to count _sides - 1 do
{
   _sides set [_i, {isPlayer _x && side _x == (_sides select _i)} count _allUnits];
};

After executing this script, the array _sides will contain the number of players in each respective side.

_sides select 0 = number of players in side west

_sides select 1 = number of players in side east

_sides select 2 = number of players in side resistance

Didn't test it tho ^^

Share this post


Link to post
Share on other sites

Thanks guys, definitely some new commands that will come in handy, although not sure if either of these scripts will work I can definitively use this new information to make my script better.

@Silderoy

The "allUnits" command should really come in handy. I used to keep track of all the players by assigning them to an array in the init/publicVariable, but this new command is much better. In this case I only wanted to count players in the trigger area so might be overkill listing through them all, but a useful command for the future just the same.

@XxAnimusxX

That script very looks useful for the "isPlayer" command. Something I was trying to do by comparing them to variables so I can cut all that part of the code out and just use isPlayer. Unless "list myTrigger" returns crew in a vehicle now I'll still need the sub-loop though. I'll do some testing and see.

-Shawn

Share this post


Link to post
Share on other sites

Maybe the command playableUnits is of interest for you, it returns, well, as it says, all playable units, which are normally always players, but could also be AI ones which are there for you to switch in them, so you would also check with isPlayer just to be sure.

Share this post


Link to post
Share on other sites

For anyone interested in doing the same this is what have come up with so far:

_numEast=0;
_numWest=0;
_numRes=0;
{
_numEast=_numEast+({isPlayer _x && side _x == east} count crew _x);
_numWest=_numWest+({isPlayer _x && side _x == west} count crew _x);
_numRes=_numRes+({isPlayer _x && side _x == resistance} count crew _x);
} forEach list _currentTrigger;

That counts the number of players per side in a trigger area (_currentTrigger) regardless of if they are in a vehicle or not. Still seems a bit inefficient but it's the best I have.

Edited by Sexacutioner

Share this post


Link to post
Share on other sites
...

That counts the number of players per side in a trigger area (_currentTrigger) regardless of if they are in a vehicle or not. Still seems a bit inefficient but it's the best I have.

Dunno if this will help, but I use this in my triggers:

{
 ({
     _x in playableUnits
 } count crew _x ) > 0
} count thislist > 0

Obviously that just checks whether there are any. If you dump the trailing ">0", it gives you "the number of players on foot + the number of vehicles with at least on player in them". Again not exactly what you want.

edit - After re-reading yours, this doesn't really add anything to the method you're using, and gives less valuable information. Oh well.

Edited by 10T

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  

×