Viikuna
No Marlo no game.
- Reaction score
- 265
Tomorrow when I woke up I had this idea bout creating a StaticTimerLoop system which would work a lil bit like Cohadars TT.
I wrote this and I would like to hear some comments and get help for finding bugs etc.
Post something now.
edit. I edited those codes a little.
edit. Added delete method for struct destroying
SystemCode:
a small example:
I wrote this and I would like to hear some comments and get help for finding bugs etc.
Post something now.
edit. I edited those codes a little.
edit. Added delete method for struct destroying
SystemCode:
JASS:
library StaticTimerLoop
globals
//=============================================================================
// This is the frequency of our timer:
public constant real PERIOD = 0.03
// This is Static Timer Loop system.
// It is used to execute some actions every PERIOD seconds.
// You might find this usefull for stuff like knockbacks and everything
// which needs high frequency timers.
//
// HOW TO USE:
//
// This system is actually pretty easy to use.
// You just need to make your struct extend StaticTimerLoop
//
// Because this system uses interfaces
// your periodict action must be named: loopAction
//
// You struct must have a method loopAction, which takes nothing returns nothing
//
// private struct Data extends StaticTimerLoop
//
// method loopAction takes nothing returns nothing
// // some actions
// endmethod
//
// endstruct
//
// To start your periodict executions you simple use boolean member Loop
//
// local Data D=Data.create() // create your Data first
//
// set D.Loop=true // This starts your timer.
// // Now method loopAction is called every PERDIOD seconds
//
// set D.Loop=false // This stops our timer.
// // method loopAction is no longer called every PERIOD seconds.
//
// To destroy your struct you need to use .delete()
// Do not use .destroy()
//
//
//
//=============================================================================
//=============================================================================
// System code:
private timer T=CreateTimer()
public integer Total=0
endglobals
private interface Face
method loopAction takes nothing returns nothing defaults nothing
endinterface
struct StaticTimerLoop extends Face
private boolean l=false
private boolean d=false
private static StaticTimerLoop array STL
private static method TimerLoop takes nothing returns nothing
local integer i=1
loop
exitwhen i>Total
if .STL<i>.l==false then
if .STL<i>.d==true then
call .STL<i>.destroy()
endif
set .STL<i>=.STL[Total]
set Total=Total-1
else
call .STL<i>.loopAction()
endif
set i=i+1
endloop
if Total==0 then
call PauseTimer(T)
endif
endmethod
method delete takes nothing returns nothing
if this.l==false then
call this.destroy()
else
set this.d=true
set this.l=false
endif
endmethod
method operator Loop takes nothing returns boolean
return this.l
endmethod
method operator Loop= takes boolean b returns nothing
if b==false then
set this.l=false
else
set this.l=true
if Total==0 then
call TimerStart(T,PERIOD,true,function StaticTimerLoop.TimerLoop)
endif
set Total=Total+1
set .STL[Total]=this
endif
endmethod
endstruct
endlibrary</i></i></i></i></i>
a small example:
JASS:
scope Test initializer Init
private struct Data extends StaticTimerLoop
integer count=0
integer endcount=0
string a=""
method loopAction takes nothing returns nothing
call BJDebugMsg(this.a)
set this.count=this.count+1
if this.count==this.endcount then
call this.delete()
endif
endmethod
method onDestroy takes nothing returns nothing
call BJDebugMsg("destroyed")
endmethod
endstruct
private function Action takes nothing returns nothing
local Data d=Data.create()
set d.a="it works"
set d.endcount=GetRandomInt(75,150)
set d.Loop=true
endfunction
private function Init takes nothing returns nothing
local trigger t=CreateTrigger()
call TriggerRegisterPlayerEventEndCinematic(t,Player(0))
call TriggerAddAction(t,function Action)
endfunction
endscope