Azlier
Old World Ghost
- Reaction score
- 461
It's like TimerUtils, but... slower. Doesn't use H2I.
Features:
Here's a test script, because actions are louder than birds, or something like that.
Only real advantage over something like KT is that you can pause the timer at any time.
Also vJassier than TimerUtils, but an alternate interface can certainly be programmed in. Major thanks to Jesus4Lyf who brought you the current version. I myself forged the idea.
Benchmark results (from Jesus, because I can't benchmark. I'm just gonna have to trust him.)
TU Red: 30100 execs/sec
TU Blue: 24700 execs/sec
VT: 22300 execs/sec
Features:
JASS:
//Static methods
vTimer.start(callback, period)
//Starts a vTimer and returns it, so you can pause it and such.
//Methods
vTimer.pause()
//Pauses a vTimer.
vTimer.resume()
//Causes a paused vTimer to resume running.
//Use "return true" when you want to end execution of a function
Here's a test script, because actions are louder than birds, or something like that.
JASS:
scope Test initializer Init
private function Callback1 takes nothing returns boolean
call BJDebugMsg("Callback1. Data: " + I2S(VT_GetTriggering().data))
return true
endfunction
private function Callback2 takes nothing returns boolean
call BJDebugMsg("Callback2. Data: " + I2S(VT_GetTriggering().data))
return true
endfunction
private function Callback3 takes nothing returns boolean
call BJDebugMsg("Callback3. Data: " + I2S(VT_GetTriggering().data))
return true
endfunction
private function Callback4 takes nothing returns boolean
call BJDebugMsg("Callback4. Data: " + I2S(VT_GetTriggering().data))
return true
endfunction
private function Actions takes nothing returns nothing
local vTimer v
call ClearTextMessages()
set v = vTimer.start(function Callback1, 0.5)
set v.data = 3
set v = vTimer.start(function Callback2, 0.25)
set v.data = 1
set v = vTimer.start(function Callback3, 1)
set v.data = 7
set v = vTimer.start(function Callback4, 0.75)
set v.data = 3
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterPlayerEvent(t, Player(0), EVENT_PLAYER_END_CINEMATIC)
call TriggerAddAction(t, function Actions)
endfunction
endscope
Only real advantage over something like KT is that you can pause the timer at any time.
Also vJassier than TimerUtils, but an alternate interface can certainly be programmed in. Major thanks to Jesus4Lyf who brought you the current version. I myself forged the idea.
Benchmark results (from Jesus, because I can't benchmark. I'm just gonna have to trust him.)
TU Red: 30100 execs/sec
TU Blue: 24700 execs/sec
VT: 22300 execs/sec
JASS:
library_once VT
globals
private integer MAX_TIMERS = 256
//Preloading timers with this is much slower than with normal TimerUtils.
//A maximum of about 600 is recommended. Besides, if you have more
//then 600 timers running at once, you're doing it wrong.
endglobals
//** System Code **
globals
private boolexpr ReleaseCond
endglobals
private function SetTriggerData takes trigger t, integer d returns nothing
if d>30 then
call SetTriggerData.execute(t,d-30)
set d=30
endif
loop
exitwhen d==0
set d=d-1
call TriggerExecute(t)
endloop
endfunction
public constant function GetTriggering takes nothing returns vTimer
return GetTriggerExecCount(GetTriggeringTrigger())
endfunction
struct vTimer
// Public:
integer data
private timer tim
private trigger trig
private boolexpr cond
private static vTimer array Stack
private static integer StackMax=0
private method stack takes nothing returns nothing
set .StackMax=.StackMax+1
set .Stack[.StackMax]=this
endmethod
private static method create takes nothing returns vTimer
local vTimer this=vTimer.allocate()
set this.tim=CreateTimer()
set this.trig=CreateTrigger()
call SetTriggerData.execute(this.trig,this)
call TriggerRegisterTimerExpireEvent(this.trig,this.tim)
call this.stack()
return this
endmethod
private static method new takes nothing returns vTimer
debug if .StackMax==0 then
debug call BJDebugMsg("vTimer ERROR: Timer limit exceeded! Check for timer leaks. If there are none, increase the timer limit.")
debug endif
set .StackMax=.StackMax-1
return .Stack[.StackMax+1]
endmethod
// Public:
method pause takes nothing returns nothing
call PauseTimer(this.tim)
endmethod
method resume takes nothing returns nothing
call ResumeTimer(this.tim)
endmethod
// Public:
static method start takes code callback, real period returns vTimer
local vTimer this=vTimer.new()
set this.cond=And(Condition(callback),ReleaseCond)
call TriggerAddCondition(this.trig,this.cond)
call TimerStart(this.tim,period,true,null)
return this
endmethod
private method release takes nothing returns nothing
call this.stack()
call PauseTimer(this.tim)
call TriggerClearConditions(this.trig)
call DestroyBoolExpr(this.cond)
set this.cond=null
endmethod
private static method releaseCond takes nothing returns boolean
call GetTriggering().release()
return false
endmethod
private static method onInit takes nothing returns nothing
set ReleaseCond = Condition(function vTimer.releaseCond)
loop
exitwhen MAX_TIMERS==0
set MAX_TIMERS=MAX_TIMERS-1
call vTimer.create()
endloop
endmethod
endstruct
public function Start takes code f, real period returns vTimer
return vTimer.start(f,period)
endfunction
endlibrary