Jump to content
hatbat

How to make part of init line repeat like a trigger condition?

Recommended Posts

I need units to ignore formation for a mission I'm working on and I've solved this by giving each unit it's own group. Unfortunately this means I've quickly hit the 144 group cap. To solve this I've come up with a trigger that allows a unit to be in the a group with others but not attempt to form a formation until it's attacked. This code requires the condition function of a trigger however I need it to instead work within the init line of each soldier as creating a trigger and name for every soldier would be impractical. This is the code as applied to a trigger (the soldier's name is unit1, who from the start has a disableAI "MOVE"):

 

CONDITION: (behaviour unit1) == "COMBAT"

 

ON ACTIVATION: unit1 enableAI "MOVE";

 

I've had success with combining this code into the init line of a soldier, though due to the nature of init lines, it only runs once at the beginning of the mission when it isn't needed. I need an example of how to make the code repeat itself until true, much like a trigger condition, though within an init line.

 

Unit's current init line, I need the if statement to repeat itself if it doesn't currently return as true. 

this disableAI "MOVE"; if ((behaviour this) == "COMBAT") then {this enableAI "MOVE"};
 

Share this post


Link to post
Share on other sites

Thanks for the reply, 

 

Hey,

 

I've done some searching and I found something on ArmaHolic forums which might help HERE

 

Good luck!

 

Chris

 

Thanks very much for the reply. Though that doesn't seem to work or at least I can't find a working method to apply that to what I'm doing. What I really need is a working example or a fixed version of my own code. 

Share this post


Link to post
Share on other sites


this disableAI "MOVE";

if (isServer) then {

    this spawn {

        params ["_unit"];

            

        while {alive _unit} do {

            

            if ((behaviour _unit) == "COMBAT") then {_unit enableAI "MOVE"};

if ((behaviour _unit) != "COMBAT") then {_unit disableAI "MOVE"};

                

            sleep 5;

        };

    };

};

Share this post


Link to post
Share on other sites

Will this be applied to ALL units of a particular side? If so you could just make one script that will run your code for all units, instead of typing it into each units init field, then call that script from init.sqf or something.

Share this post


Link to post
Share on other sites

derived from the solution of davidoss I would suggest u can avoid some while loops if u run it at leaders init line only.

That should work because I guess the leader of a group sets the behaviour of all members.

you can do it with a script which u name handle_move.sqf

then u can compile it as function in initServer.sqf with this:

 

fnc_handle_movement = compileFinal preprocessFile "handle_move.sqf";
ur handle_move.sqf is this:

params ["_unit"];

private _grp = group _unit;
private _last_behavior ="";
private _behavior ="";

while {alive _unit} do 
{
 _behavior = behaviour _unit;

 if(_behavior != _last_behavior) then
 {
  _last_behavior = _behavior;

  if (_behavior == "COMBAT") then 
  {
   {
    _x enableAI "MOVE";
   } forEach units _grp;
  }
  else
  {
   {
    _x disableAI "MOVE";
   } forEach units _grp;
  };
 };
 sleep 5;
};

if((count units _grp) > 0) then 
{
 [(leader _grp)] spawn fnc_handle_movement;
};
 

then u can do this at ur group leaders init line:

 

if (isServer) then
{
 this spawn 
 {
  params ["_unit"];

  waitUntil{sleep 5; !(isNil "fnc_handle_movement")};
  [_unit] spawn fnc_handle_movement;
 };
};
Maybe it looks much more complicated as the solution of davidoss but u will have only one while loop for each group instead for each unit.

Another advantage is that it sets the movement only if behavior is changed.

EDIT: updated code block to working version

Edited by sarogahtyp
  • Like 1

Share this post


Link to post
Share on other sites

Modified from davidoss suggestion:

//Call from initServer.sqf

this disableAI "MOVE";
if (isServer) then {
  {
    _x spawn {

        params ["_unit"];
            
        while {alive _unit} do {
            
            if ((behaviour _unit) == "COMBAT") then {_unit enableAI "MOVE"}
            else {_unit disableAI "MOVE"};
                
            sleep 5;
        };
    };
  } forEach allUnits;
};

Untested, but something like that should work.

Share this post


Link to post
Share on other sites

Modified from davidoss suggestion:

//Call from initServer.sqf

this disableAI "MOVE";
if (isServer) then {
  {
    _x spawn {

        params ["_unit"];
            
        while {alive _unit} do {
            
            if ((behaviour _unit) == "COMBAT") then {_unit enableAI "MOVE"}
            else {_unit disableAI "MOVE"};
                
            sleep 5;
        };
    };
  } forEach allUnits;
};
Untested, but something like that should work.

The first line will throw an error I believe, so remove that and it shouldn't be a problem.

Share this post


Link to post
Share on other sites

hatbat asked me via PM to get that code snippets of my prior post working.

I had some typos in it but now it works. I updated my prior post and here is the dropbox link:

 

https://www.dropbox.com/s/iwqebbhw2339ls0/handle_move_test.Altis.rar?dl=0

 

its a 2D Editor mission.

@hatbat:

I tested the mission and it seems to work.

But there is a thing which I did not test.

If the group leader dies then the new leader should handle the movement. u have to test if that works...

  • Like 1

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

×