• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Snippet] NewTimerData

This little snippet makes it so that you can handle all your timer data attaching stuff on one line and also release/get the timer data with just one line.


Instead of:
JASS:
local timer t = NewTimer()
call SetTimerData(t, this)
call TimerStart(t, 2.00, false, function thistype.callback)
set t = null

It becomes:
JASS:
call TimerStart(NewTimerData(this), 2.00, false, function thistype.callback)

And with a timer that only expires once, you might normally do this:

JASS:
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
call ReleaseTimer(t)
set t = null

It becomes:

JASS:
local thistype this = PopExpiredTimer()

It's also modular with structs to keep the code-length short and sweet:

JASS:
method destroy takes nothing returns nothing
    call ReleaseTimer(this.timer)
    call this.deallocate()
endmethod
...
static method callback takes nothing returns nothing
    call thistype(PopExpiredTimer()).destroy()
endmethod
...
static method create takes nothing returns thistype
    local thistype this = thistype.allocate()
    set this.timer = NewTimerData(this)
    call TimerStart(this.timer, 2.00, false, function thistype.callback)
    return this
endmethod


Here's the short code:

JASS:
library NewTimerData requires TimerUtils
    globals
        private timer t
        private integer j
    endglobals
    function NewTimerData takes integer i returns timer
        set t = NewTimer()
        call SetTimerData(t, i)
        return t
    endfunction
    function PopExpiredTimer takes nothing returns integer
        set t = GetExpiredTimer()
        set j = GetTimerData(t)
        call ReleaseTimer(t)
        return j
    endfunction
endlibrary
 
Last edited:
Level 8
Joined
Oct 3, 2008
Messages
367
There's really not much to gain, and at the cost of a tiny bit of efficiency. You're shaving, what, two lines of short code?

I'm not able to judge this one singlehandedly. Some comments would be of aid to me.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Timers with attached data are normally tied in with events so that they are stopped in case the data on it becomes invalid. Without being tied in with events, the functions themselves would require validation (thus making everything messier).

It's easier to create different timer functions for different sets of data rather than just a generic one as 9 times out of 10 you'll need to do stuff with it besides start it up.

AIDS Unit Indexing is a system that believes in stopping timers upon their expiration rather than stopping them upon invalidation (locks), thus only having to start the timer as the function already contains the necessary validations. But who actually uses locks besides j4l?

Most coders will stop a timer as soon as it is no longer valid rather than waiting for it to expire and validate the data on it. Many ORPG/RPG maps like TKoK follow this ideal... hell, the only programmer I've ever seen who hasn't follow this has been j4l : P.

Invalid data would be things like timers running buffs that no longer exist or damaging units that are gone.

You have my thoughts : ).
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
JASS:
    function PopExpiredTimer takes boolean release returns integer
        local integer i
        set t = GetExpiredTimer()
        set i = GetTimerData(t) // Thanks to purge for pointing my mistake.

        if release then
            call ReleaseTimer(t)
        endif
        
        return i
    endfunction

Maybe for people like me who needs the timer to be periodic? I'm not talking about high frequency application, I'm talking about repeating the timer every 10 seconds or so.
 
Last edited:
JASS:
    function PopExpiredTimer takes boolean release returns integer
        local integer i
        set t = GetExpiredTimer()
        
        if release then
            set i = GetTimerData(t)
            call ReleaseTimer(t)
        endif
        
        return i
    endfunction

Maybe for people like me who needs the timer to be periodic? I'm not talking about high frequency application, I'm talking about repeating the timer every 10 seconds or so.

JASS:
function PopExpiredTimer takes boolean release returns integer
    local integer i
    set t = GetExpiredTimer()
    set i = GetTimerData(t)

    if release then
        call ReleaseTimer(t)
    endif
    
    return i
endfunction

;D
 
Level 12
Joined
Mar 28, 2005
Messages
160
I like it. Things that make my life easier are good. And some of the best ideas often end up being the simplest in execution.

but why name the second function

JASS:
PopExpiredTimer
??

I hope you don't expect people to use it for data retrieval in periodic intervals (though you provide for that functionality)...that would be silly...really this only seems useful for one-outs, otherwise you're only saving a single line.
 
Last edited:
Top