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

Simple question about vJass

Status
Not open for further replies.
Level 7
Joined
Nov 19, 2015
Messages
283
Hi
Just learning how to attach data to timers in jass.
I am following this tutorial here:
http://www.thehelper.net/threads/jass-timers-and-how-to-pass-data-to-them.106986/


JASS:
private function TimerLoop takes nothing returns nothing
     local integer i=0
     loop
        exitwhen i>=Total
        if D[i].end then // end is a boolean which tells us when to stop calling actions
             call D[i].destroy()
             set Total=Total-1
             if Total > 0 then
                 set D[i]=D[Total]
                 set i=i-1
             else
                 call PauseTimer(T)
             endif
        else
            call D[i].action()
        endif
        set i=i+1
     endloop
endfunction

This code is for removing the instances from the array. I don't understand why you reduce total by 1 first and then set D=D[Total].
Shouldn't it be set D=D[Total+1]?
 
Level 3
Joined
Apr 17, 2008
Messages
41
"Total" is the total number of indexes used (including index zero).

Example:

This means that if indexes 0-9 are allocated, then "Total" is equal to 10.

When an index is being removed, you set "Total" to 9 and then set the removed index to the last index in the array (which would be 9 in this case).
 
Level 7
Joined
Oct 19, 2015
Messages
286
Since we're talking vJass, why not use a library instead of writing the code from scratch? Maybe something like this.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
You can use TimerUtils. It does exacly what you are asking for.

To study the mechanics you should look into TimerUtilsEx.
It's the same library, but a bit more readable and optimized.
It's in the graveyard, because the original TimerUtils offers the same functionality.

The API is very easy
JASS:
private function TimerExpire takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer data = GetTimerData(t)// Get the integer value. Here 102.
    
    call ReleaseTimer(t)// Don't forget to push to timer back to the timer stack.
    
    set t = null
endfunction

private function Start takes nothing returns nothing
    local integer data = 102
                    // NewTimerEx(integer) pops a timer from the timer stack
                    // and saves "integer" to it's handle id.
    call TimerStart(NewTimerEx(data), false, 1., function TimerExpire)
endfunction
 
Level 13
Joined
Nov 7, 2014
Messages
571
It turns out there is a timer "exploit" that allows you to attach data to a timer without using any other storage, i.e just the timer itself.
It was apparently known by the Russian mod making community since 2007. Kudos to
whoever found it =). It doesn't work for a timer with a periodic flag set to true, but that's not really a problem.

JASS:
globals
    timer interlooper = CreateTimer()
    integer user_data = 39
endglobals

function attach_data_to_timer_exploit takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer data = R2I(TimerGetRemaining(t) + 0.5)
    local real timeout = TimerGetTimeout(t)

    // Do stuff with data...
    call BJDebugMsg("data: " + I2S(data))

    // Need a periodic timer?
    if data != 42 then
        set data = data + 1

        call TimerStart(t, I2R(data), false, null)
        call PauseTimer(t)
        call TimerStart(t, timeout, false, function attach_data_to_timer_exploit)
    endif
endfunction

function attach_data_to_timer_start_exploit takes nothing returns nothing
    call TimerStart(interlooper, I2R(user_data), false, null)
    call PauseTimer(interlooper)
    call TimerStart(interlooper, 3.14159, false, function attach_data_to_timer_exploit)
endfunction
 
Status
Not open for further replies.
Top