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

Can you give a simple explain of how works timers in Jass?

Status
Not open for further replies.
In jass, you can create timers, call a function when a timer expires (once or repeating) and from a function being called by a timer, you can get the timer that expired to trigger that function.

What problems did you have?
You want to store information from the moment you setup the timer to when the function is being called?
Use a hashtable with the timers handleId as key and what-ever-you-want as value or use a library to help you with that (Timer (easier than TimerUtils), TimerUtil, [System] TimerUtilsEx are a few known onces).
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
More or less same as GUI really. Of course it is written differently but functionality is the same, just more convenient as you do not need to create a separate trigger.

edit: you can also attach data to a timer thanks to a known exploit which can be pretty useful as this means you can get the timer to get attached to a struct instance for example.
Same can be done with a hashtable but waste of a slot.
 
Level 11
Joined
May 29, 2008
Messages
132
You should look for the library "TimerUtils" for Jass. AFAIK it's the standard library used for better timer recycling and associating data with timers.

That said, Timers are something Lua is much better at than Jass, since you can bind functions as callbacks from TimerStarts. It's much easier to persist data into these functions, since they can access variables from when the timer was started.

(Or, as I prefer, use Typescript that is compiled to Lua! It's great!)
 
Level 24
Joined
Jun 26, 2020
Messages
1,853
You should look for the library "TimerUtils" for Jass. AFAIK it's the standard library used for better timer recycling and associating data with timers.

That said, Timers are something Lua is much better at than Jass, since you can bind functions as callbacks from TimerStarts. It's much easier to persist data into these functions, since they can access variables from when the timer was started.

(Or, as I prefer, use Typescript that is compiled to Lua! It's great!)
I'm in the 1.26
 
Level 4
Joined
Jul 26, 2017
Messages
66
If you're just looking for an example, here is a code I am currently working on:

JASS:
function Trig_Meditation_Conditions takes nothing returns boolean
    if ( not ( GetSpellAbilityId() == 'A032' ) ) then
        return false
    endif
    return true
endfunction

function ZenCheck takes unit monk returns boolean
    if GetUnitState(monk, UNIT_STATE_LIFE) < GetUnitState(monk, UNIT_STATE_MAX_LIFE) then
        return false
    endif
    if (GetUnitState(monk, UNIT_STATE_MANA) < GetUnitState(monk, UNIT_STATE_MAX_MANA)) then
        return false
    endif
    return true
endfunction

function ZenTick takes nothing returns nothing
    local timer cl = GetExpiredTimer()
    local unit w = LoadUnitHandle (udg_HT_self, GetHandleId(cl), 57)
    if ( GetUnitCurrentOrder(w) == String2OrderIdBJ("corporealform") ) then
        call SetUnitState (w, UNIT_STATE_LIFE, (GetUnitState(w, UNIT_STATE_LIFE) + (GetUnitState(w, UNIT_STATE_MAX_LIFE) / 20 ) ) )
        call SetUnitState (w, UNIT_STATE_MANA, (GetUnitState(w, UNIT_STATE_MANA) + (GetUnitState(w, UNIT_STATE_MAX_MANA) / 20 ) ) )
    if ZenCheck(w) == true then
        call IssueImmediateOrder (w, "holdposition")
    endif
    else
        call PauseTimer (cl)
        call DestroyTimer (cl)
        call FlushChildHashtableBJ( GetHandleId(cl), udg_HT_self )
        call BJDebugMsg ("timer cleared")
    endif
    set cl = null
    set w = null
endfunction

function Trig_Meditation_Actions takes nothing returns nothing
    local timer ck
    if (ZenCheck(GetTriggerUnit()))== true then
        call IssueImmediateOrder (GetTriggerUnit(), "holdposition")
    else
        set ck = CreateTimer()
        call SaveUnitHandle (udg_HT_self, GetHandleId(ck), 57, GetTriggerUnit())
        call TimerStart ( ck, 1.00, true, function ZenTick )
    endif
    set ck = null
endfunction

//===========================================================================
function InitTrig_Meditation takes nothing returns nothing
    set gg_trg_Meditation = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Meditation, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( gg_trg_Meditation, Condition( function Trig_Meditation_Conditions ) )
    call TriggerAddAction( gg_trg_Meditation, function Trig_Meditation_Actions )
endfunction

ask anything. hope this helps. (any coding feedback is also appreciated)
EDIT: wrote better code
 
Last edited:
Status
Not open for further replies.
Top