Jump to content
Leopard20

Activating a trigger multiple times for each unit

Recommended Posts

Hey guys.

 

I was wondering what is the best and most efficient way to run a trigger's code for each unit/object that satisfies its condition?

 

(Note that the units might be AI. Not all of them are players. Also note that not all units activate the trigger at the same time)

1. The obvious method, of course, is to create a trigger for every unit in the game. But I think it might hurt the game performance (especially if there's lots of units and there is more than one area to cover).

 

2. Another way is to use waitUntil (or while, but I've found waitUntil to be more performance-friendly) instead of a trigger:

waitUntil {
	sleep 1;
	{
		if (_x distance2D _triggerPos < 50) then {
			if !(_x getVariable ["TriggerActivated", false]) then {[_x, _trigger] spawn _code1;_x setVariable ["TriggerActivated", true]};
		} else {
			if (_x getVariable ["TriggerActivated", false]) then {[_x, _trigger] spawn _code2;_x setVariable ["TriggerActivated", false]};
		};
	} forEach allUnits;
	false
};

 

3. The third (hypothetical) way is to get the magic variable "thisList" somehow (assuming it updates frequently and there's actually a way to get it). I'm not sure if this one is feasible though.

 

 

Which one do you prefer?

 

Also, if you have any additional ideas, I'd appreciate it if you could share it with me!

Share this post


Link to post
Share on other sites

Update: I think this is more optimized than the 2nd one:
2. Keeping the trigger, but spawning this code from the activation code:

waitUntil {
	sleep 1;
	{
		if (_x distance2D _triggerPos < 50) then {
			[_x, _trigger] spawn _code1;
		} else {
			[_x, _trigger] spawn _code2;
		};
	} forEach allUnits;
	!(triggerActivated _trigger)
};

 

Share this post


Link to post
Share on other sites

You should explain what you intend to do rather than bit of codes, triggers or not. There are multiple solutions, for all units, for players, for checking distance or checking a presence in area (see inArea)...

A code optimization starts with a clear statement of the aim.

  • Like 1

Share this post


Link to post
Share on other sites

@pierremgi
OK. What I want to create is a "Flag Capture" script. Units enter this trigger area one by one, trying to capture it. Now I'm supposed to know who's in the cap radius to determine how the cap status is changing. And this is exactly what I'm looking for a solution for.

 

P.S: This area command seems useful: "positions inAreaArray trigger"
Thanks for the tip!

Share this post


Link to post
Share on other sites

you can have a repeatable trigger, server only or not depending on how the result is used further,  anybody present, area you want:

in condition field:

this && isNil "togglingTrig"

 

in on act field:

togglingTrig = true;

hint format ["blue: %1\nred: %2",west countSide thisList, east countSide thisList];   // example

 

in on deact field:

0 = [] spawn {sleep 2; togglingTrig = nil}     // making the trigger "rearmed" each 2 seconds.

 

Share this post


Link to post
Share on other sites
32 minutes ago, pierremgi said:

you can have a repeatable trigger, server only or not depending on how the result is used further,  anybody present, area you want:

in condition field:

this && isNil "togglingTrig"

 

in on act field:

togglingTrig = true;

hint format ["blue: %1\nred: %2",west countSide thisList, east countSide thisList];   // example

 

in on deact field:

0 = [] spawn {sleep 2; togglingTrig = nil}     // making the trigger "rearmed" each 2 seconds.

 

Does that mean even if one unit leaves the trigger area it will be deactivated? Because otherwise there's no way it can get "rearmed".

Share this post


Link to post
Share on other sites

Update: Turns out you can get units that satisfy the condition simply by using "list _trigger". My bad.

Share this post


Link to post
Share on other sites
46 minutes ago, Leopard20 said:

Does that mean even if one unit leaves the trigger area it will be deactivated? Because otherwise there's no way it can get "rearmed".

That means you have an updated list/count in my example, every 2 sec, as far as there is somebody inside the trigger. If no one, the trigger is just waiting for an entrance, as usual.

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

×