HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

a little timer system.

02-08-2009, 01:21 PM#1
dorreen
I tried to make a timer system which makes it easy to execute some actions every x seconds.

It is basicly just some struct array and a interface which allows me to have different actions in order to make this work for many different spell at the same time.

I would like get some feedback about how good this idea actually is and how should I improve this and stuff like that.

Here is the code:
Collapse 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

The reason why I use method operators is because I wanted to try how they work. I believe that some nice .start and .stop -methods would fit better.

What do you think?

A small example code:
Collapse 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 
02-08-2009, 03:01 PM#2
Anitarf
You mean something like this?
02-08-2009, 03:11 PM#3
dorreen
I have used Cohadars TT and I tried to make this work similiar way.

But no, I dont mean any of those systems. I just want to know if this method is good enought and should I try to make a nice system out of it.
02-08-2009, 06:31 PM#4
Anitarf
How is it different, though? What's the advantage of writing it instead of using one of those?
02-08-2009, 06:45 PM#5
dorreen
I was hoping you would tell me that.


I guess I just like to write these things.
02-10-2009, 02:53 AM#6
fX_
n.m.

p.s. if all these scripts work the same way (functionally and operationally, like all track circuit competitors run to the finish line and do so by running in their lane in the track) then maybe somebody should just pick the best and keep it; or at least rate each according to efficacy.
02-10-2009, 04:13 PM#7
dorreen
Actually, quraji has a pretty cool system which works pretty much as I intended this to work. I guess im just going to use his system now, so Im not probably going to edit this anymore.