Jump to content
.Marti

Creating Triggers with ForEach HELP

Recommended Posts

Hi guys, i'm trying to create a trigger for certain number of groups but i'm so lost. 

 

First of all i have an array of groups called _groupsConnected, each of them has an undetermined amount of units inside. 

 

I want to create a trigger for each of the groups. This is what i'm trying, i can figure that the problem is with the _x variables but i can not find a solution. 

 

Quote

{
    _surrenderTrigger = createTrigger ["EmptyDetector", getPos player, false];
    _surrenderTrigger setTriggerActivation ["NONE", "PRESENT", false];
    _surrenderTrigger setTriggerArea [0, 0, 0, true];
    _surrenderTrigger setTriggerStatements 
    [
        "( {alive _x} count units _x) < 2",  //condition: When there is only 1 unit alive. 
        "{_x setCaptive true; 
        _x action ["Surrender", _x];
        } forEach units _x",  //activation
        ""
    ];

} forEach _groupsConnected;

 

This is meant to be executed server side only. Maybe there is an easier way to do this instead of using triggers. 

 

Any idea is very welcome. Thanks!

 

Share this post


Link to post
Share on other sites

The solution could be a global variable wherein u store all groups.

In your setTriggerStatements-part u can't use the _x of the outer scope in the strings because that _x is not known there on the time of execution.

You have to use a global variable to get the desired groups there.

Share this post


Link to post
Share on other sites

Alternative solution:

In initServer.sqf (or init.sqf  with isServer condition)

 

0 = [] spawn {
  while {true} do {
    private ["_remainingUnits","_units"];
    uisleep 2;
    {
      _remainingUnits = units _x select {alive _x};
      if (count _remainingUnits == 1) then {
        _x setVariable ["surrended",TRUE];
        _unit = _remainingUnits #0;
        if (objectParent _unit isKindOf "landVehicle") then {
          moveOut _unit;
          _remainingUnits allowGetIn FALSE;
        };
        if (local _unit) then {
          _unit setCaptive TRUE;
          _unit switchMove "";
          _unit playMoveNow "AmovPercMstpSsurWnonDnon";
        } else {
          [_unit, {
            _this setCaptive TRUE;
            _this switchMove "";
            _this playMoveNow "AmovPercMstpSsurWnonDnon";
          }] remoteExec ["call",_unit];
        };
      }
    } foreach (allGroups select {isnil {_x getVariable "surrended"} && side _x != civilian});
  };
};

You can replace allGroups by _groupsConnected  and remove the condition for side.

Share this post


Link to post
Share on other sites
5 hours ago, sarogahtyp said:

The solution could be a global variable wherein u store all groups.

In your setTriggerStatements-part u can't use the _x of the outer scope in the strings because that _x is not known there on the time of execution.

You have to use a global variable to get the desired groups there.

 

Thanks for your response @sarogahtyp, I went a little bit further with scopes etc following your advice and found this post with various aportations:

Subsequently I edited my initial code with the following:

 

Spoiler
Quote

params ["_logic","_groupsConnected","_surrenderLimit"];
// _groupsConnected = [grupo1];
// _surrenderLimit = 100;

{
    //Surrender calculations
    _surrenderPercentage = _surrenderLimit/100;
    _surrenderCapRaw = (count units _x) * _surrenderPercentage;
    _surrenderCap = round _surrenderCapRaw;
    if (_surrenderCap < 2) then {_surrenderCap = 2};
    
    //create trigger/s
    _surrenderTrigger = createTrigger ["EmptyDetector", getPos _logic, false];
    
    //pass variables
    _surrenderTrigger setVariable ["SurrenderCap",_surrenderCap];
    _surrenderTrigger setVariable ["GrupoActual",_x];
    
    //define trigger
    _surrenderTrigger setTriggerActivation ["NONE", "PRESENT", false];
    _surrenderTrigger setTriggerArea [0, 0, 0, true];
    _surrenderTrigger setTriggerStatements 
    [
        "({alive _x} count units (thisTrigger getVariable 'GrupoActual')) < (thisTrigger getVariable 'SurrenderCap')",  //condition: When there is only X units alive.
        "{
        _x setCaptive true; 
        _x action ['Surrender', _x];
        } forEach units (thisTrigger getVariable 'GrupoActual')",  //activation
        ""
    ];

} forEach _groupsConnected;

true

 

 

It works now!!

 

Thanks for the alternative solution aswell @pierremgi

Btw, what i'm trying to do is a modules addon for my comunity and i'm learning A LOT with your MGI_Modules. Hope i'm not breaking any authors rights and you are confortable with a scripting newbie learning with it! 😛



 

Share this post


Link to post
Share on other sites
2 hours ago, .Marti said:

Thanks for the alternative solution aswell @pierremgi

Btw, what i'm trying to do is a modules addon for my comunity and i'm learning A LOT with your MGI_Modules. Hope i'm not breaking any authors rights and you are confortable with a scripting newbie learning with it! 😛



 

 

No problem with that. Simple words in credits if you think about it. (I'm late for new version). I can help you anyway.

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

×