- Joined
- Mar 19, 2008
- Messages
- 3,141
Old.
Version 3.0.0.0
Simplifies adding and removing conditions.
- renamed again to RefreshTrigger
- code passed through parameter, no longer has to return false
- support for remove condition has been added
- events are no longer removed
Demo code:
To fill hole from lacking remove-event native. By no means it removes event itself since it's impossible - instead, it replaced given trigger and reapplies data from previous one.
Note:
- All actions have to be applied via conditions in order to restore trigger properly
- code added as condition must return false
- Does not supportTriggerRemoveCondition
native
Tell me if support for TriggerRemoveCondition should be given - can upload 2nd version for those who really need it. Although TriggerRemoveNative is not in much of a use since it requirestriggercondition
instead ofboolexpr
as a paramether.
Version 3.0.0.0
Simplifies adding and removing conditions.
- renamed again to RefreshTrigger
- code passed through parameter, no longer has to return false
- support for remove condition has been added
- events are no longer removed
JASS:
/*****************************************************************************
*
* RefreshTrigger v3.0.1.0
* by Bannar aka Spinnaker
*
* Simplifies trigger refresh process i.e. adding and removing conditions, as never before.
*
******************************************************************************
*
* Requirements:
* Alloc - choose whatever you like
* ListTemplate by Bannar
* hiveworkshop.com/forums/submissions-414/containers-list-t-249011/
*
* Optional:
* Table by Bribe
* hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
*
******************************************************************************
*
* Important:
* All actions have to be applied via conditions in order to refresh trigger properly
*
******************************************************************************
*
* struct Trigger:
*
* readonly trigger trigger
* retrieves trigger handle
*
* static method create takes nothing returns thistype
* default ctor
*
* method destroy takes nothing returns nothing
* default dctor
*
* method empty takes nothing returns boolean
* whether trigger has any conditions applied or not
*
* method conditionCount takes nothing returns integer
* count of currently added conditions
*
* method refresh takes nothing returns nothing
* replaces this trigger with newly created one and reapplies it's conditions
*
* method addCondition takes code c returns nothing
* adds condition to this trigger
*
* method removeCondition takes thistype node returns nothing
* removes condition node from this trigger
*
* method clearConditions takes nothing returns nothing
* clears conditions from this trigger
*
*****************************************************************************/
library RefreshTrigger uses /*
*/ Alloc /*
*/ ListTemplate /*
*/ optional Table
private struct ConditionItem extends array
triggercondition condition
boolexpr bexpr
implement Alloc
static method create takes triggercondition tc, boolexpr b returns thistype
local thistype this = thistype.allocate()
set this.condition = tc
set this.bexpr = b
return this
endmethod
method destroy takes nothing returns nothing
set condition = null
set bexpr = null
call deallocate()
endmethod
endstruct
//! runtextmacro DEFINE_LIST("ConditionList", "ConditionItem", "true", "0", "false", "")
private module TriggerInit
private static method onInit takes nothing returns nothing
static if LIBRARY_Table then
set table = Table.create()
endif
endmethod
endmodule
struct Trigger extends array
private ConditionList list
readonly trigger trigger
private trigger callback
implement Alloc
static if LIBRARY_Table then
private static Table table
else
private static hashtable table = InitHashtable()
endif
private static method evaluate takes nothing returns boolean
static if LIBRARY_Table then
return TriggerEvaluate( table.trigger[ GetHandleId(GetTriggeringTrigger()) ] )
else
return TriggerEvaluate( LoadTriggerHandle(table, 0, GetHandleId(GetTriggeringTrigger()) ) )
endif
endmethod
private method synchronize takes nothing returns nothing
static if LIBRARY_Table then
set table.trigger[GetHandleId(trigger)] = callback
else
call SaveTriggerHandle(table, 0, GetHandleId(trigger), callback)
endif
endmethod
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set this.list = ConditionList.create()
set this.trigger = CreateTrigger()
set this.callback = CreateTrigger()
call this.synchronize()
call TriggerAddCondition(this.trigger, function thistype.evaluate)
return this
endmethod
method destroy takes nothing returns nothing
static if LIBRARY_Table then
call table.handle.remove(GetHandleId(trigger))
else
call RemoveSavedHandle(table, 0, GetHandleId(trigger))
endif
call deallocate()
call list.destroy()
set list = 0
call DestroyTrigger(trigger)
call DestroyTrigger(callback)
set trigger = null
set callback = null
endmethod
method empty takes nothing returns boolean
return list.empty()
endmethod
method conditionCount takes nothing returns integer
return list.size()
endmethod
private method findNode takes boolexpr b returns ConditionListNode
local ConditionListNode node = list.first
loop
exitwhen node == 0 or node.data.bexpr == b
set node = node.next
endloop
return node
endmethod
method refresh takes nothing returns nothing
local ConditionListNode node = list.first
call DestroyTrigger(callback)
set callback = CreateTrigger()
loop
exitwhen node == 0
set node.data.condition = TriggerAddCondition(callback, node.data.bexpr)
set node = node.next
endloop
call synchronize()
endmethod
method addCondition takes code c returns nothing
local triggercondition tc
local boolexpr b
if ( c != null and trigger != null ) then
set b = Condition(c)
set tc = TriggerAddCondition(callback, b)
call list.pushBack( ConditionItem.create(tc, b) )
set b = null
set tc = null
endif
endmethod
method removeCondition takes code c returns nothing
local ConditionListNode node
local boolexpr b
if ( c != null and trigger != null ) then
set b = Condition(c)
loop
set node = findNode(b)
exitwhen node == 0
call TriggerRemoveCondition(callback, node.data.condition)
call list.delete(node)
endloop
set b = null
endif
endmethod
method clearConditions takes nothing returns nothing
call list.clear()
call TriggerClearConditions(callback)
endmethod
implement TriggerInit
endstruct
endlibrary
JASS:
struct RestoreTriggerTest extends array
static Trigger trig
private static method testme takes nothing returns boolean
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "This trigger handle id: "+I2S(GetHandleId(trig.trigger)))
call CreateUnit(Player(0),'hfoo',0,-100,0)
return false
endmethod
private static method onInit takes nothing returns nothing
set trig = Trigger.create()
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Type \"s\" to summon a footman at your side. Two conditions.")
call TriggerRegisterPlayerChatEvent(trig.trigger, Player(0), "s", true )
call trig.addCondition(function thistype.testme)
call trig.addCondition(function thistype.testme)
call PolledWait(5.)
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Conditions have been removed.")
call trig.removeCondition(function thistype.testme)
call PolledWait(5.)
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "A single condition has been added.")
call trig.refresh()
call trig.addCondition(function thistype.testme)
endmethod
endstruct
Last edited: