[Snippet] Checking if units have become aware of the players

Party-approved future science plus handbooks for the revolution
Post Reply
User avatar
Posts: 1208
Joined: Wed May 25, 2011 4:59 pm

[Snippet] Checking if units have become aware of the players

Post by wolfenswan » Tue Jul 08, 2014 2:12 pm

While the detected by Trigger usually does the job there might be instances where it isn't ideal to use, e.g. you only want to check if specific units or the enemy in generals has become aware of the players

This can be done by using the knowsAbout command. As it returns a numerical value it can easily be used in combination with count to check a whole array of units. KnowsAbout is useful because it doesn't increase instantly (as for example checking for nearby shots would) and thus allows for a "grace period" where players can dispose of a unit without alerting enemies.

For each playable unit we check if the vehicle VehCSAT_AH1 knows about it (>0). If it knows about any unit (the second > 0) the line of code will return true.
(({VehCSAT_AH1 knowsAbout _x > 0} count PlayableUnits) > 0
This can be extended easily:
((({VehCSAT_AH1 knowsAbout _x > 0} count PlayableUnits) + (({VehCSAT_AH2 knowsAbout _x > 0} count PlayableUnits))) > 0
Now it adds the knowsAbout values for both VehCSAT_AH1 and VehCSAT_AH2 and if any of them knowsAbout (total value > 0), it returns true.

But what if we wanted to check for a whole group of units?
({_u = _x; {_u knowsAbout _x > 0} count PlayableUnits} forEach units GrpAAF_Patrol) > 0
This line loops through all units of the group named GrpAAF_Patrol and checks the knowsAbout value. If at least one of them knowsAbout players (second >0) then it returns true.

Neatly, knowsAbout can also be used with SIDE instead of a unit:
(INDEPENDENT knowsAbout _x > 0} count PlayableUnits) > 0;
In this case it would check for all independent units.


The easiest way to use this is in a trigger (that isn't activated otherwise) and use the "On Activation" field to have happen whatever should happen if the players are detected. With a timeout value you can also increase the "grace period" for the players.

Again, using the code for the AAF Patrol:
({_u = _x; {_u knowsAbout _x > 0} count PlayableUnits} forEach units GrpAAF_Patrol) > 0
And in the on-act field:
{_x setCombatMode "RED"; } forEach (units GrpAAF_Patrol);
This would cause the entire group to go full alert. You can also use the reveal command to update their knowledge of targets to make them engage actively.

KnowsAbout is a fluent value. You don't need to check if it's > 0 but can use all the values on the "scale". Freshly spotted units will always have at least 1.5 but from there on out it varies depending on distance, weather and time of day. Only units within 400m on a clear day will have the highest "knowsAbout value" of 4. I suggest using the debug console to monitor AI's knowsAbout state if you want to tweak the values.

Post Reply