Nestharus
o-o
- Reaction score
- 84
Introduction
Essentially does this, but safely, meaning you can't remove triggerconditions from triggers where the triggercondition is actually on a different trigger or something.
JASS:integer count = 0 trigger t = CreateTrigger() //onAdd set count = count + 1 call TriggerAddCondition(t, c) //onRemove set count = count - 1 call TriggerRemoveCondition(t, tc) //onEvaluate if count > 0 then call TriggerEvaluate(t) endif
So that you can do this to save an evaluation if the trigger doesn't have code in it
JASS:Trigger t = Trigger.create() integer tc //onAdd set tc = t.add(c) //onRemove call t.remove(tc) //onEvaluate if (t.hasCode) then call t.fire() endif
JASS:
library Trigger
//v2.0.1.0
//Trigger is used to evaluate a trigger if that trigger has code in it.
//It can safely add/remove code from triggers.
//
//-module TriggerMod
// Implement this to create your own Trigger struct
//
// -static method create takes nothing returns thistype
// Creates a trigger.
//
// -method destroy takes nothing returns nothing
// Destroys trigger.
//
// -method add takes boolexpr c returns integer
// Adds code to trigger.
//
// -method remove takes integer c returns nothing
// Removes code from trigger.
//
// -method operator hasCode takes nothing returns boolean
// Returns a boolean determining whether the trigger has code in it or not.
//
// -method clear takes nothing returns nothing
// Clears all code out of trigger.
//
// -method fire takes nothing returns boolean
// Fires the trigger.
//
//struct Trigger
// Shared Trigger struct. Implements TriggerMod.
globals
private hashtable table = InitHashtable()
private integer triggerType = 0
endglobals
module TriggerMod
private static integer instanceCountE = 0
private static integer array recycleE
private trigger t
private integer countX
private static integer recycleCountE = 0
private static integer thisType = 0
private boolean allocated
private integer instanceCount
private integer recycleCount
public method operator hasCode takes nothing returns boolean
return countX != 0
endmethod
public method fire takes nothing returns boolean
return TriggerEvaluate(t)
endmethod
public static method create takes nothing returns thistype
if (recycleCountE != 0) then
set recycleCountE = recycleCountE - 1
set thistype(recycleE[recycleCountE]).allocated = true
return recycleE[recycleCountE]
endif
set instanceCountE = instanceCountE + 1
set thistype(instanceCountE).allocated = true
set thistype(instanceCountE).t = CreateTrigger()
return instanceCountE
endmethod
public method destroy takes nothing returns nothing
if (allocated) then
call TriggerClearConditions(t)
set countX = 0
set recycleE[recycleCountE] = this
set recycleCountE = recycleCountE + 1
call FlushChildHashtable(table, thisType*8192+this)
call FlushChildHashtable(table, -thisType*8192-this)
set instanceCount = 0
set recycleCount = 0
set allocated = false
endif
endmethod
public method clear takes nothing returns nothing
if (allocated) then
set countX = 0
set instanceCount = 0
set recycleCount = 0
call TriggerClearConditions(t)
call FlushChildHashtable(table, thisType*8192+this)
call FlushChildHashtable(table, -thisType*8192-this)
endif
endmethod
public method add takes boolexpr c returns thistype
local thistype tc = 0
if (allocated and c != null) then
if (recycleCount != 0) then
set tc = LoadInteger(table, -thisType*8192-this, recycleCount)
set recycleCount = recycleCount - 1
else
set instanceCount = instanceCount + 1
set tc = instanceCount
endif
set countX = countX + 1
call SaveTriggerConditionHandle(table, thisType*8192+this, tc, TriggerAddCondition(t, c))
endif
return tc
endmethod
public method remove takes thistype c returns nothing
local triggercondition tc = LoadTriggerConditionHandle(table, thisType*8192+this, c)
if (allocated and tc != null) then
call TriggerRemoveCondition(t, tc)
set countX = countX - 1
call RemoveSavedHandle(table, thisType*8192+this, c)
call SaveInteger(table, -thisType*8192-this, recycleCount, c)
set recycleCount = recycleCount + 1
endif
endmethod
private static method onInit takes nothing returns nothing
set thisType = triggerType
set triggerType = triggerType + 1
endmethod
endmodule
struct Trigger extends array
implement TriggerMod
endstruct
endlibrary