[ZINC] Starting Timers?

tooltiperror

Super Moderator
Reaction score
231
So, I decided to dip my fingers into two concepts: ZINC and Timers. So, for practice, I used Weep's advice and made a basic timers system. I want to know how to make it more accurate, or just more complicated in general to improve my knowledge of it.

JASS:

  //!  zinc

library ZINC
{
    constant integer ArraySize=20;
    //Maximum Size for Arrays.
    
    hashtable Hash;
    integer TimerCount;
    timer Stack[ArraySize];
    
    function StartTimer(integer structint, real time, boolean periodic, code handler) -> timer
    {
          if (TimerCount==0)
          {
              Stack[1]=CreateTimer();
              TimerCount=1;
          }
        SaveInteger(Hash,0,GetHandleId(Stack[TimerCount]),structint);
        TimerStart(Stack[TimerCount],time,periodic,handler);
        TimerCount=TimerCount-1;
        return Stack[TimerCount];
    }
    
    function GetStructData(timer t) -> integer
    {
        return LoadInteger(Hash,0,GetHandleId(t));
    }
    
    function EndTimer(timer t)
    {
        if(t==null)
        {
            PauseTimer(t);
            TimerCount=TimerCount+1;
            Stack[TimerCount]=null;
        }
    }
}

  //! endzinc
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Apparently, you didn't test your script.

Some informations for Zinc :
1) All functions, structs, variables libraries are private members for default.
2) Struct members are public for default.

For your script :
1) You won't able to call StartTimer,GetStructData,EndTimer at outside of library because they are private members.
2) "if(t==null)" fails. You recycler is bugged.
3) No need to declare custom array size, because it will be slower due to function calls. Use [] is enough.
4) Use Table instead of hashtable(since you are using 1 array only), because blizzard leaves a lame hashtable limitation for us.

JASS:
//! zinc
library Test {

    timer TimerStack [];
    integer Count = 0;
    hashtable hasht = InitHashtable();
    constant integer RANDOM = 0x10000D;
    
    public function StartTimer (code func, integer data, real period) {
        if (Count == 0) {
            TimerStack[0] = CreateTimer();
        } else {
            TimerStack[0] = TimerStack[Count];
            Count -= 1;
        }
        SaveInteger(hasht,0,GetHandleId(TimerStack[0]),data);
        TimerStart(TimerStack[0],period,true,func);
    }
    
    public function TimerGetData () -> integer {
        return LoadInteger(hasht,0,GetHandleId(GetExpiredTimer()));
    }
    
    public function DiscardTimer () {
        if (TimerGetData() != RANDOM) {
            TimerStack[0] = GetExpiredTimer();
            PauseTimer(TimerStack[0]);
            SaveInteger(hasht,0,GetHandleId(TimerStack[0]),RANDOM);
            Count = Count + 1;
            TimerStack[Count] = TimerStack[0];
        } else {
            debug BJDebugMsg("Double free of timer blocked!");
        }
    }
}
//! endzinc

Table version :
JASS:
//! zinc
library Test requires Table {

    timer TimerStack [];
    integer Count = 0;
    constant integer RANDOM = 0x10000D;
    HandleTable ht;


    public function StartTimer (code func, integer data, real period) {
        if (Count == 0) {
            TimerStack[0] = CreateTimer();
        } else {
            TimerStack[0] = TimerStack[Count];
            Count -= 1;
        }
        ht[TimerStack[0]] = data;
        TimerStart(TimerStack[0],period,true,func);
    }
    
    public function TimerGetData () -> integer {
        return ht[GetExpiredTimer()];
    }
    
    public function DiscardTimer () {
        if (TimerGetData() != RANDOM) {
            TimerStack[0] = GetExpiredTimer();
            PauseTimer(TimerStack[0]);
            ht[TimerStack[0]] = RANDOM;
            Count = Count + 1;
            TimerStack[Count] = TimerStack[0];
        } else {
            debug BJDebugMsg("Double free of timer blocked!");
        }
    }
    
    function onInit () {
        ht = HandleTable.create();
    }
}
//! endzinc
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top