• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Dynamically creating and destroying triggers ?

Status
Not open for further replies.

Ardenian

A

Ardenian

JASS:
function Trig_NewTrigger_Actions takes nothing returns nothing
    //actions
endfunction

//==== Init Trigger NewTrigger ====
function InitTrig_NewTrigger takes nothing returns nothing
    set gg_trg_NewTrigger = CreateTrigger()
    call TriggerRegisterTimerEventPeriodic( gg_trg_NewTrigger, 2 )
    call TriggerAddAction(gg_trg_NewTrigger, function Trig_NewTrigger_Actions)
endfunction

Would it be appropriate to destroy a trigger dependent on conditions ?

JASS:
if udg_TempUnit == null then
    call DestroyTrigger(gg_trg_NewTrigger)
endif

and to re-create the trigger manually if needed ?

JASS:
call ExecuteFunction("InitTrig_NewTrigger")

I haven't tested whether that works, but I would like to know what is the
general statement and personal opinions to this, as I would use such a combination for a system concept then.
 

Ardenian

A

Ardenian

It is most likely a periodic trigger not requiring to exist under certain situations.
As triggers run, no matter if activated or not, when having a periodic event.
 

Ardenian

A

Ardenian

Hm, so I start the trigger manually outside the trigger and if I need to pause it, then
I pause it within the trigger via PauseTimer using a condition, I see.

Thank you!

Though, would the other way be appropriate, too ?
/ Would the re-creation of the trigger work ?
 
Level 23
Joined
Feb 6, 2014
Messages
2,466
Though, would the other way be appropriate, too ?
/ Would the re-creation of the trigger work ?

I think so (haven't tested), but it would be much less efficient because
  • You're using ExecuteFunc
  • You're re-creating a trigger
  • You're re-creating a trigger event
  • You're re-adding a trigger action

As triggers run, no matter if activated or not, when having a periodic event.
Is that true?
 

Ardenian

A

Ardenian

Hm I see, so I stay with the timer solution, thank you!
 
It would be better to just use DisableTrigger. That prevents the trigger's events from firing. As soon as you want the trigger to be able to fire again, just use EnableTrigger.

But as Flux said, if this is explicitly for periodic triggers, then you can pretty much always replace it with a timer (except in weird cases where you need TriggerSleepAction(...)).
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
To get back to your original question.
Yes triggers can be created dynamically, but there are some rules you have to know.
In your example however I recommend using either Timers or the DisableTrigger native.
For my own code I prefer timers here.

Dynamic triggers are useful when a trigger becomes useles in the process.
For example you register an EVENT_UNIT_DEATH event and the unit dies.
Better example you register an EVENT_UNIT_DAMAGED event and the unit is removed.

In general for dynamic triggers TriggerAddAction is prohibited.
It will not run a proper internal cleanup, instead you need TriggerAddCondition.

It's maybe dangerous to destroy a trigger before all registered triggerconditions
of the internal list were evaluated.
 

Ardenian

A

Ardenian

@PurgeandFire
Someone wrote some time ago a trigger being disabled still runs periodically,
but is not executed. So DisableTrigger would actually not make a difference, performance-wise, would it ?

@Ofel
Thanks, I take a look on it. Though I myself am going to have only one
or another timer up to now, let's see whether I can make greater use of it.

@BPower
So I cannot piece together a trigger, using TriggerAddAction, from
different action functions, for example ?

Thank you, guys!
 
A trigger does not run anymore when disabled. What still runs is the trigger event, which is usually unproblematic.
What happens is that the trigger will not react anymore to any events which are triggered, when it's disabled.

Now, this can be a bit confusing for the "Everey 1 seconds" event, because one might think that each trigger uses it's own event, which also gets enabled/disabled with the trigger is... but it's not.
The event will still fire behind the scenes, and work for other "Every X seconds" - triggers just normaly.

So when you turn your trigger off, and after a random duration on again, then you don't know when this event will fire next again.
It won't be exactly after "1" second, but somewhere random between "0" and "1" second, even the event is "Every 1 seconds".
 

Ardenian

A

Ardenian

Ah I see, thank you, IcemanBo,
so that's another reason to use 0.03 in GUI and a counter.

Could a peridoic event cause problems if there are many many events being periodic ?
Would it then be wise ot destroy triggers not being useful/used anymore ?
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
Why can't you just use a timer? I mean it fires a callback function after it has expired. I remember requesting help for testing that , IIRC it doesn't pause the trigger's timer, luckily the demomap is still available.
 
One should destroy "temporary" handles. But if you create triggersin GUI, they are just global and static, so you might re-use them.
No really need to destroy them, especially if there is a chance that it needs to be re-created some later.

Trigger destruction is actually only a matter when it comes do dynamicly creating them, during runtime, for a system.

Also, what is very important for GUI + destroy triggers. Only TriggerConditions will be destroyed automatically on trigger destruction.
TriggerActions need to be destroyed explicitly destroyed with TriggerRemoveAction, which is impossible in GUI.. so it will always leak in GUI: http://www.hiveworkshop.com/forums/spells-569/wain-spellpack-v1-0-a-276917/#post2801818
 

Ardenian

A

Ardenian

Hm, I think this drifted off. I myself am interested whether I can dynamically create
a trigger and piece it together with using TriggerAddAction.
I assume it adds actions in the same order as the function TriggerAddAction was called.

Ah I see, thanks IcemanBo, good to know!
I do use Jass by the way, excuse I started with GUI.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
Hm, I think this drifted off. I myself am interested whether I can dynamically create
a trigger and piece it together with using TriggerAddAction .
I assume it adds actions in the same order as the function TriggerAddAction was called.
It's ( probably ) a linked list. What is added first executes first.

If you would see it in JASS it looks a bit like this:
JASS:
actionFunction node = firstNode// List of registered trigger actions.
loop
    exitwhen node == 0
    // Here comes the part that executes the action function.
    set node = node.next
endloop
 

Ardenian

A

Ardenian

Ah I see, thank you BPower! I am going to play a bit with it around
 
Status
Not open for further replies.
Top