No announcement yet.

Check Alive Status of A Lot of Individual Objects - For Beginners to Scripts

  • Filter
  • Time
  • Show
Clear All
new posts

  • [GUIDE] Check Alive Status of A Lot of Individual Objects - For Beginners to Scripts

    If you have a mission where say there are a lot of individual objects or entities that you want to check if they are alive or not then there are several ways to do this. Some of these ways can be more efficient than others.

    For this example we are going to have 20 objects (could be enemy infantry, some could be ammo boxes and some might be vehicles). They are going to be named in the editor t1 t2 t3 t4 ect...all the way to t20.

    The standard way to do so in the editor can cause a lot of processing each CPU cycle. This is when you create a trigger and in the condition you just put this code:
    !alive t1 && !alive t2 && !alive t3 && !alive t4 && !alive t5 && !alive t6 && !alive t7 && !alive t8 && !alive t9 && !alive t10 && !alive t11 && !alive t12 && !alive t13 && !alive t14 && !alive t15 && !alive t16 && !alive t17 && !alive t18 && !alive t19 && !alive t20
    This trigger will run continuously as much as it can and will take some time to process all 20 conditions each time. Most likely, it is not essential to your mission that you check this condition as often as is possible. However, in a trigger there is no way to tell it to only check the condition every few seconds.

    So we can then run this as a script instead, and then just pass the result to each client when all our targets are no longer alive. The easy way to do this is to store all of your object names in an array like this:

    _allMyTargets = [t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20];
    {DO THIS CODE WHERE _x IS t1 then t2 then t3 ect...}forEach _allMyTargets;
    Then we use the forEach command to cycle through all of the elements in the array with repeated code. The variable _x will be equal to t1 first. Then it will be equal to t2 and so on until all the elements of the array have been passed to the code contained in the {} brackets that precede the forEach command.

    Here is the way we can do this in our example mission.

    You have the objects named t1 to t20 and they are all objects that can be "alive".

    You create a trigger with no area that will run on the server only and in the condition box replace "this" with "true". In the On Activation box type the following:
    handle = []execVM "serverCheckTwentyEntities.sqf";
    This will then run the script we will create when the server starts this mission.

    Next, in the mission folder create a new text document and name it serverCheckTwentyEntities.sqf and open it in your text editor (I use Notepad ++). Copy and paste in this code and save it.

    if !(isServer) exitWith {};
    _allMyTargets = [t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20];
    _run = true;
    targetsAlive = "true";
    publicVariable "targetsAlive";
    _x = t1;
    while {_run} do
        _anyTargetsAlive = false;
            //check if any of the targets are alive
            if (alive _x) then 
                _anyTargetsAlive = true;
            //if none of the targets are alive then broadcast that to all connected and future clients
            if !(_anyTargetsAlive) then
                targetsAlive = "false";
                publicVariable "targetsAlive";
                _run = false;
        }forEach _allMyTargets;
     sleep 2;
    Back in the editor place another trigger with no area and do not check the server only box. We want this one to run on all connected clients. This will create the action we want to happen when all these entities are no longer alive. In this case it will display some text in the middle of the screen.

    In the condition:
    targetsAlive == "false"
    In the On Activation:
    titleText ["Those entities are really really dead!","PLAIN",10];

    So that is it. Adjust the last trigger to do what you want. This will only run on the server and will only check every two seconds. In the second last line of the script there is the "sleep 2;" where the script will wait two seconds each time through the loop before it checks all 20 entities again for being alive or not. This may help your missions performance if you have a lot going on in it.It will be far more efficient than a simple trigger.
    |TG-189th| Unkl
    ArmA 3 Game Officer
    Dean of Tactical Gamer University
    189th Infantry Brigade Member
    "We quickly advance in the opposite direction and take cover in a house on the SW side of town." - BadStache



TeamSpeak 3 Server


Twitter Feed