Solmyr
Ultra Cool Member
- Reaction score
- 30
This should do better than your current version.
JASS:
library PeriodicLoop
globals
private constant real TIMEOUT = 0.03125
private timer mainTimer = CreateTimer()
private trigger mainTrigger = CreateTrigger()
private integer N = 0
endglobals
struct PeriodicLoop
private thistype next
private thistype prev
stub method onPeriodicLoop takes nothing returns nothing
return
endmethod
private static method onExpire takes nothing returns boolean
local thistype this = thistype(0).next
loop
exitwhen (this == 0)
call this.onPeriodicLoop()
set this = this.next
endloop
return false
endmethod
public static method stopPeriodicLoop takes thistype t returns nothing
set t.prev.next = t.next
set t.next.prev = t.prev
call t.destroy()
set N = N - 1
if (N == 0) then
call PauseTimer(mainTimer)
endif
endmethod
private static method tick takes nothing returns nothing
call TriggerEvaluate(mainTrigger)
endmethod
public static method startPeriodicLoop takes thistype t returns nothing
set thistype(0).next.prev = t
set t.next = thistype(0).next
set thistype(0).next = t
set t.prev = thistype(0)
set N = N + 1
if (N == 1) then
call TimerStart(mainTimer, TIMEOUT, true, function PeriodicLoop.tick)
endif
endmethod
private static method onInit takes nothing returns nothing
call TriggerAddCondition(mainTrigger, Condition(function PeriodicLoop.onExpire))
endmethod
endstruct
endlibrary