Jump to content
Sign in to follow this  
davidoss

Help on trigger condition.

Recommended Posts

Hi.

I have the code which is toggling the nvg usage night /day .

It works but only if i with my troops come trough this time  condition.

If i connect at night and recruit troops then it is not working anymore because it has been already fired at my connect and i need to wait for the next onact/deact swich.

// autonvg for each unit while night
private ["_autonvg"];
_autonvg = createTrigger ["EmptyDetector", [0, 0, 0], false];
_autonvg setTriggerStatements [
	"daytime > 4 AND daytime > 20 AND {(leader (group player)) == player}",
	"
	private ['_unit', '_nvgItem', '_nvgs'];
		{
			_unit = _x;

			if (alive _unit) then {
				
					_nvgItem = '';

					_nvgs = ['rhsusf_ANPVS_14','rhsusf_ANPVS_15','ACE_NVG_Gen1','ACE_NVG_Gen2','NVGoggles','ACE_NVG_Gen4','ACE_NVG_Wide'];

					{if (_x in (items _unit)) exitWith {_nvgItem = _x}} forEach _nvgs;

					_unit assignItem _nvgItem;
			};
		}
		forEach (units (group player));
	",
	"
	private ['_unit', '_nvgItem', '_nvgs'];
		{
			_unit = _x;

			if (alive _unit) then {
				
					_nvgItem = '';

					_nvgs = ['rhsusf_ANPVS_14','rhsusf_ANPVS_15','ACE_NVG_Gen1','ACE_NVG_Gen2','NVGoggles','ACE_NVG_Gen4','ACE_NVG_Wide'];

					{if (_x in (assignedItems _unit)) exitWith {_nvgItem = _x}} forEach _nvgs;
					
					_unit unassignItem _nvgItem; 

			};
		}
		forEach (units (group player));
	"
];

Any idea how to change the trigger statements?

Share this post


Link to post
Share on other sites

I didn't exactly change your trigger statements, but I thought this would be a good opportunity to use the new command arrayIntersect, so here is my result. In addition, I changed your condition because it didn't work for me reliably.

_nvgs = ['rhsusf_ANPVS_14','rhsusf_ANPVS_15','ACE_NVG_Gen1','ACE_NVG_Gen2','NVGoggles','ACE_NVG_Gen4','ACE_NVG_Wide'];
while{true} do
{
    if ((dayTime > 20) || (dayTime >=0 && dayTime <= 4)) then
    {
        {
            _itemsPlayer = items _x;
            _commonItemsArray = _nvgs arrayIntersect _itemsPlayer;
            _nvg = _commonItemsArray select 0;
            if(!isNil "_nvg") then
            {
                _x assignItem _nvg;
            };
        } forEach allPlayers;    
    }
    else
    {
        {
            _itemsPlayer = assignedItems _x;
            _commonItemsArray = _nvgs arrayIntersect _itemsPlayer;
            _nvg = _commonItemsArray select 0;
            if(!isNil "_nvg") then
            {
                _x unassignItem _nvg;
            };
        } forEach allPlayers;    
    };
    sleep 3600;
};

I personally don't like using trigger statments, especially not for long scripts like yours.

 

Tell me what you think.

 

Edit: I removed the hints, because they were for testing only.

  • Like 1

Share this post


Link to post
Share on other sites

Thank you for your  answer. This is awesome idea but there is going about AI units. Player can easy  handle it self. Can you look one more time please?

Share this post


Link to post
Share on other sites

What exactly is the issue with AIs? Should the nvgs also be removed from all AIs in player's group?

Share this post


Link to post
Share on other sites

You could tray adding another variable to the condition and flip the state between true and false forcing the trigger to rerun.

 

in an init setup a variable.

 

react=true

 

then in your condition

daytime > 4 AND daytime > 20 AND {(leader (group player)) == player and react}

 

Now somewhere you need to flip the sate, it may work if you place it after the first lump of code :-

 

forEach (units (group player));;react=false;null=[] spawn {sleep 0.6;react=true};

 

However the condition will now be checked every 0.6th of a second and could cause performance hit

  • Like 1

Share this post


Link to post
Share on other sites

What exactly is the issue with AIs? Should the nvgs also be removed from all AIs in player's group?

 

AI only.

Share this post


Link to post
Share on other sites

Do the same thing as above, only this time forEach allUnits with a check for !isPlayer:

{
  if (!isPlayer) then
  {
    //NVG removal code
  };
} forEach allUnits;

Alternatively, try some "math":

{
  //NVG removal code
} forEach (allUnits - allPlayers);

(Both code samples are untested.)

Share this post


Link to post
Share on other sites

This should do the trick.

_nvgs = ['rhsusf_ANPVS_14','rhsusf_ANPVS_15','ACE_NVG_Gen1','ACE_NVG_Gen2','NVGoggles','ACE_NVG_Gen4','ACE_NVG_Wide'];
while{true} do
{
    if ((dayTime > 20) || (dayTime >=0 && dayTime <= 4)) then
    {
        {
            if(isPlayer _x) then
            {
                _itemsPlayer = items _x;
                _commonItemsArray = _nvgs arrayIntersect _itemsPlayer;
                _nvg = _commonItemsArray select 0;
                if(!isNil "_nvg") then
                {
                    _x assignItem _nvg;
                };
            };
        } forEach allUnits;    
    }
    else
    {
        {
            if(isPlayer _x) then
            {
                _itemsPlayer = assignedItems _x;
                _commonItemsArray = _nvgs arrayIntersect _itemsPlayer;
                _nvg = _commonItemsArray select 0;
                if(!isNil "_nvg") then
                {
                    _x unassignItem _nvg;
                };
            };
        } forEach allUnits;    
    };
    sleep 3600;
};

Share this post


Link to post
Share on other sites

Wow thanks.

 

I do not understand the condition:

if(!isNil "_nvg") then

there will be never empty _nvg  from :

_nvg = _commonItemsArray select 0;

Correct me please if i am wrong.

Share this post


Link to post
Share on other sites
_nvg = _commonItemsArray select 0;

This can be empty if there are no common items in _nvgs and _itemsPlayer, but I depends on your mission, if you can be sure that the player always has got one of the items from _nvgs then its obsolete.

 

Simple example from the editor:

array = [];

nilVariable =  array select 0;

isNil "nilVariable" returns true

Share this post


Link to post
Share on other sites

I have expanded it little bit for each sides and maybe it working. Need to wait until the code fired.
 
R3vo thanks for the awesome code and  info about the new command >arrayIntersect<.

// adds and remove nvgs 
private ["_nvgsw", "_nvgse", "_nvgsi", "_itemsUnit", "_commonItemsArray", "_nvg", "_nvgrw", "_nvgre", "_nvgri"];
_nvgsw = ["rhsusf_ANPVS_14", "rhsusf_ANPVS_15", "NVGoggles", "ACE_NVG_Gen4", "ACE_NVG_Wide"];
_nvgse = ["ACE_NVG_Gen1", "NVGoggles_OPFOR"];
_nvgsi = ["ACE_NVG_Gen2", "NVGoggles_INDEP"];

while{true} do
{
    if ((dayTime > 20) || (dayTime >=0 && dayTime <= 4)) then

    {
        {
			if (side _x == WEST and !isPlayer _x and _x isKindOf "Man") then
				{
					if(alive _x) then
					{
						_itemsUnit = (items _x + assignedItems _x);
						_commonItemsArray = _nvgsw arrayIntersect _itemsUnit;
						_nvg = _commonItemsArray select 0;
						_nvgrw = _nvgsw call BIS_fnc_selectRandom;
					
						if(!isNil "_nvg") then
							
							{
								if !(_nvg in (assignedItems _x)) then
								{
								_x assignItem _nvg;
								};
								
							} else {
							
								_x linkitem _nvgrw;
								
							};
					};
				};
				
				
			if (side _x == EAST and !isPlayer _x and _x isKindOf "Man") then
				{
					if(alive _x) then
					{
						_itemsUnit = (items _x + assignedItems _x);
						_commonItemsArray = _nvgse arrayIntersect _itemsUnit;
						_nvg = _commonItemsArray select 0;
						_nvgre = _nvgse call BIS_fnc_selectRandom;
					
						if(!isNil "_nvg") then
							
							{
								if !(_nvg in (assignedItems _x)) then
								{
								_x assignItem _nvg;
								};
							} else {
							
								_x linkitem _nvgre;
								
							};
					};
				};

			
			if (side _x == INDEPENDENT and !isPlayer _x and _x isKindOf "Man") then
				{
					if(alive _x) then
					{
						_itemsUnit = (items _x + assignedItems _x);
						_commonItemsArray = _nvgsi arrayIntersect _itemsUnit;
						_nvg = _commonItemsArray select 0;
						_nvgri = _nvgsi call BIS_fnc_selectRandom;
					
						if(!isNil "_nvg") then
							
							{
								if !(_nvg in (assignedItems _x)) then
								{
								_x assignItem _nvg;
								};
							} else {
							
								_x linkitem _nvgri;
								
							};
					};
				};
				
        } forEach allUnits;
    }
    else
    {
        {
			if (side _x == WEST and !isPlayer _x and _x isKindOf "Man") then
			{
				if(alive _x) then
				{
					_itemsUnit = assignedItems _x;
					_commonItemsArray = _nvgsw arrayIntersect _itemsUnit;
					_nvg = _commonItemsArray select 0;
					
						if(!isNil "_nvg") then
							
							{
								_x unassignItem _nvg;
								
						};
				};
			};

			
			if (side _x == EAST and !isPlayer _x and _x isKindOf "Man") then
			{
				if(alive _x) then
				{
					_itemsUnit = assignedItems _x;
					_commonItemsArray = _nvgse arrayIntersect _itemsUnit;
					_nvg = _commonItemsArray select 0;
					
						if(!isNil "_nvg") then
							
							{
								_x unassignItem _nvg;
								
						};
				};
			};


			if (side _x == INDEPENDENT and !isPlayer _x and _x isKindOf "Man") then
			{
				if(alive _x) then
				{
					_itemsUnit = assignedItems _x;
					_commonItemsArray = _nvgsi arrayIntersect _itemsUnit;
					_nvg = _commonItemsArray select 0;
					
						if(!isNil "_nvg") then
							
							{
								_x unassignItem _nvg;
								
						};
				};
			};
			
			
        } forEach allUnits;
    };
    sleep 1800;
};
  • Like 1

Share this post


Link to post
Share on other sites

Your script looks fine to me, well done!

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  

×