• 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.

JASS Timers

Status
Not open for further replies.
Level 8
Joined
Jul 3, 2011
Messages
251
Hey guys, I'm trying to make a spell but it requires that i use JASS and timers, as I'm sure you can tell, I'm new to JASS. I've been searching all over different threads and posts but none of them have been usefull to me. So, how do you use timers to momentarily pause a loop? :grin:
 
Using timers in jass is simply replacement for standard GUI loop with event Time - every 0.0X seconds.

You start timer using:
JASS:
native TimerStart           takes timer whichTimer, real timeout, boolean periodic, code handlerFunc returns nothing
Where variables are: timer itself, Interval, boolean - represents if timer is repeatable or one shoot type - true for repeating, false for not. Last one is actually a function that will be called whenever given timer expires. Remember that function called this way have to 'take nothing return nothing'.

Mostly, you start a timer in action function and create additional was that will execute the actions.
JASS:
function Loop takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local unit u = LoadUnitHandle(yourHashtable, GetHandleId(t), 1)
    call KillUnit(u)
    call FlushChildHandle(yourHashtable, GetHandleId(t))
    call PauseTimer(t)
    call DestroyTimer(t)
    set t = null
    set u = null
endfunction

function Actions takes nothing returns nothing
    local timer t = CreateTimer()
    local unit u = GetTriggerUnit()
    call SaveUnitHandle(yourHashtable, GetHandleId(t), 1, u)
    call TimerStart (t, 5.00, false, function Loop)
    set t = null
    set u = null
endfunction

This will actually create a timer that after 5 seconds will kill given unit, thats ofcourse just an egzample and not a whole trigger, because you will need init trigger and other stuff to make it work.
Imporant things: in loop function remember to refer to expired timer via GetExpiredTimer(). When loop is ended, and the leak-clearing part comes, you have to pause given timer before destroying it. Null non real/integer/boolean/string variables at the end of functions. Don't null variables before actually destroying/removing it in case you will destroy nothing.

JASS:
function Loop takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer id = GetHandleId(t)
    local unit u = LoadUnitHandle(yourHashtable, id, 1)
    local real duration = LoadReal(yourHashtable, id, 2)
    if duration <= 0 then
        call KillUnit(u)
        call FlushChildHandle(yourHashtable, id)
        call PauseTimer(t)
        call DestroyTimer(t)
    else
        call SaveUnitHandle(yourHashtable, id, 2, duration - 0.03125)
    endif
    set t = null
    set u = null
endfunction

function Actions takes nothing returns nothing
    local timer t = CreateTimer()
    local unit u = GetTriggerUnit()
    local integer id = GetHandleId(t)
    local real duration = 5.00
    call SaveUnitHandle(yourHashtable, id, 1, u)
    call SaveUnitHandle(yourHashtable, id, 2, duration)
    call TimerStart (t, 0.03125, true, function Loop)
    set t = null
    set u = null
endfunction

^Above simple loop egzample. Function will check periodicaly if duration has ended, if so given unit will be killed.
 
timers are very usefull loop-wait function, but you can use a loop inside a timer loop also which is very useful when used in a situation when you arrayed some variables...

sample;
this loop displays Player number 1 to 12 per second until it reached greater than 3 which is the condition...
JASS:
globals
    integer LOOPCOUNT = 0
endglobals

function Trig_Untitled_Trigger_002_Actions takes nothing returns nothing
    local integer i = 0
    loop
        call BJDebugMsg("Player " + I2S(GetPlayerId(Player(i))+1))
        set i = i + 1
        exitwhen i >= 12
    endloop
    set LOOPCOUNT = LOOPCOUNT+1
    call BJDebugMsg("loop count is " + I2S(LOOPCOUNT))
endfunction

function cond takes nothing returns boolean
    return LOOPCOUNT < 3 
endfunction
//===========================================================================
function InitTrig_Untitled_Trigger_002 takes nothing returns nothing
    set gg_trg_Untitled_Trigger_002 = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Untitled_Trigger_002, 1.00 )
    call TriggerAddCondition( gg_trg_Untitled_Trigger_002, Condition(function cond))
    call TriggerAddAction( gg_trg_Untitled_Trigger_002, function Trig_Untitled_Trigger_002_Actions )
endfunction
 
Status
Not open for further replies.
Top