• 🏆 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!

[Snippet] TimerData


JASS:
library TimerData
//====================================
//
//  This is a fast and lightweight way of attaching values to timers.
//
//  It works off an exploit that functions by starting a timer with the 
//  value you want to be stored as the timeout, then immedietly pausing the timer.
//  Then once you start the timer again, TimerGetRemaining will return the initial value - 0.5.
//
//  The problem with this exploit is that it doesn't work with periodic timers, though you can
//  simply start the timer again in the callback function to bypass this limitation.
//
//  Alternitvely you can use the RefreshTimer function.
//
//  Example:
//      // main function
//            call TimerStartEx(CreateTimer(), TIMEOUT, function callback, timerdata.create())
//      // callback
//            local timer whichTimer = GetExpiredTimer()
//            local timerdata data   = TimerGetData(twhichTimer) // get the attached data
//            call RefreshTimer(whichTimer, function callback).
//
//  Credits to XGM.ru for discovering this bug.  
//
//====================================

    function TimerGetData takes timer whichTimer returns integer 
        return R2I(TimerGetRemaining(whichTimer) + 0.5)
    endfunction
    
    function RefreshTimer takes timer whichTimer, code handlerFunc returns nothing
        call TimerStart(whichTimer, TimerGetTimeout(whichTimer), false, handlerFunc)
    endfunction
    
    function TimerStartEx takes timer whichTimer, real timeout, code handlerFunc, integer data returns nothing 
        call TimerStart(whichTimer, data, false, null)
        call PauseTimer(whichTimer)
        call TimerStart(whichTimer, timeout, false, handlerFunc)
    endfunction
    
endlibrary
 
Last edited:
Top