• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[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