Jump to content
Sign in to follow this  
alleycat

Script counting casualties?

Recommended Posts

Are there scripts available that count how many of a given side or defined list of units are dead or alive?

---------- Post added at 02:45 AM ---------- Previous post was at 02:43 AM ----------

After looking at the comref I found this:

unit countEnemy array

Operand types:
   unit: Object
   array: Array
Type of returned value:
   Number
Description:
   Counts how many units in the array are considered enemy by the given unit.

Example:
   player countEnemy list triggerOne

Category: OFP 

How to "print" the result of this in lets say a hint command?

Share this post


Link to post
Share on other sites
hint str (player countenemy allunits)

This prints out always 0, even though there are enemies of my side inside the trigger area.

Share this post


Link to post
Share on other sites

dead units are sent to side of civilian. only way to count the dead is to have an array of all units at mission start, then find units at a later time and the difference would be the dead. Problem though is this would count any units that died and not just one side. Here is a script that is floating around the forums which deletes dead units. Works really well and should help you with your problem. Script will need to be ran on each client. So if you put it into your init.sqf in the global section as follows

_xhandle = execvm "clearCorpses.sqf";

private ["_aU", "_dU"];
_aU = allUnits;

while {true} do {

if (count _aU != count allUnits) then {
	_dU = _aU - allUnits;
	{hidebody _x} foreach _dU;
	sleep 2;
	{deletevehicle _x} foreach _dU;
};
_aU = allUnits;
sleep 600;
};

Share this post


Link to post
Share on other sites

Better is to use killed EH:

Put this to every unit's init:

this addEventHandler ["killed", {0 execVM "casualtyCount.sqf"}];

The zero is the side's ID. Which side will have which number is up to you to choose. You can, for example, use: west = 0; east = 1; napa = 2; civ = 3

init.sqf:

ALLEY_deadcount = [0,0,0,0]; //Put as many zeros as you have sides.

casualtyCount.sqf:

_side = _this;

_count = ALLEY_deadcount select _side;
_count = _count + 1;
ALLEY_deadcount set [_side, _count];

Does it need to be MP compatible?

Edited by Deadfast

Share this post


Link to post
Share on other sites

Even just a working trigger that counts the amount of living units of one side would do it.

Yes for MP.

Share this post


Link to post
Share on other sites

MP compatibility edit:

init.sqf:

if (isNil "ALLEY_deadcount") then //Make sure we don't overwrite anything if we JIP
{
    ALLEY_deadcount = [0,0,0,0]; //Put as many zeros as you have sides.
};

if (isServer) then //Server-only part
{
    onPlayerConnected '[] execVM "JIP.sqf"';
};

JIP.sqf:

publicVariable "ALLEY_deadcount"; //Update the just-connected client

casualtyCount.sqf:

_side = _this;

_count = ALLEY_deadcount select _side;
_count = _count + 1;
ALLEY_deadcount set [_side, _count];
publicVariable "ALLEY_deadcount"; //Broadcast to all clients.

Share this post


Link to post
Share on other sites
Even just a working trigger that counts the amount of living units of one side would do it.

Yes for MP.

hint str (EAST countside allunits)

Share this post


Link to post
Share on other sites

Deadfast, that looks great.

I want to take another step and use the counter to end the mission (as losing) if a certain number of friendlies die. I'm new to scripting but here is what I tried and it's not working.

I made a script called CheckCasualtyCounter.sqf and have it run in all playable units init line

_side = _this;


while {(alive player)} do
{
if ((ALLEY_deadcount select _side) > 5) then 
{
	endMission "loser";
} else
{
	sleep 10;
};
};

if (true) exitWith {};

If anyone could correct this it would be greatly appreciated along with any other info to help implement this. Thanks.

Share this post


Link to post
Share on other sites

Just add end trigger with that condition: ALLEY_deadcount select whateverside > 5

Share this post


Link to post
Share on other sites
Just add end trigger with that condition: ALLEY_deadcount select whateverside > 5

Yeah I thought of that but I was trying to do it with a script so it only checks the number every 10 seconds instead of how many ever times per second. Trying to save resources... and learn more scripting :)

Triggers should have a "check condition timer" that allows you to set how often the trigger checks for the condition.

Share this post


Link to post
Share on other sites
Yeah I thought of that but I was trying to do it with a script so it only checks the number every 10 seconds instead of how many ever times per second. Trying to save resources... and learn more scripting

Triggers should have a "check condition timer" that allows you to set how often the trigger checks for the condition.

You are probably on the right track with your line of thinking...if I understand all the dynamics corrrectly, a trigger that only has a condition of this is polled every .5 seconds. So, if it is a simple east/present trigger and only says this in the condition, it will poll once every .5 secs and be pretty easy on the cpu (unless the detection area is very big and there are lot of units/vehicles moving in and out or near the trigger borders).

But, if there is any other condition in the Condition line, the trigger polls for the condition once per frame (just like a script). So, a condition like this && alive player or even alive player or other boolean check will poll every frame. Here is an old OFP post from Suma to support this (again, assuming I am interpreting it correctly):

"@" vs. "delayed if-loop"

(I have a collection of other links of "triggers vs scripts" from OFPEC as well that I believe support this theory:

LAG factor, trigger v.s sqf

Max # of scripts in a mission

Triggers or scripts? (Performance-wise)

Now, if you only have a few triggers, unless your condition is really cpu-intensive (like using nearestObjects over a huge detection area) triggers are a quick and easy way of checking your condition...but if you you have a lot of triggers, or complex conditions, or no need to poll frequently a script may be the better way to go.

Share this post


Link to post
Share on other sites

Thanks for the input.

The following trigger condition works great:

(ALLEY_deadcount select 0) > 10;

I'm not sure if that would be polled every .5 sec or at each frame. If it's only .5 sec that would be fine.

Anyway, I am trying to do it with a script, but need a pro to check it over. It works but seems to have issues:

while {(alive player)} do
{
if ((ALLEY_deadcount select 0) > 10) then 
{
	endMission "loser";
} else
{
	sleep 10;
};
};

if (true) exitWith {};

It is working in SP testing, but I'm not sure what is best for multiplayer. Also, the sleep for 10 seconds doesn't appear to be working because the reaction of the tenth guy to die seems to be instant (i will test this more). I just want the script to check the casualty count once every 10 seconds. And be MP compatible.

Share this post


Link to post
Share on other sites

End is instant if last/all human player dies.

Your code will end the mission in 0-10 seconds after the limit is reached. It doesn't sleep 10 after it has been reached.

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  

×