• 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 5
Joined
Jul 17, 2006
Messages
145
is there a jass function similar to

native TimerStart takes timer whichTimer, real timeout, boolean periodic, code handlerFunc returns nothing

except instead of requiring a function as the last parameter, you can put code in. I would like to use a local var, and i would be able to do that if i could use parameters with the function myfunction code, but i cant, so i just want to use raw code. anyway, iv tryed doing this, and it returns a syntax error: Cannot create code from nothing. - if i cant do this can i at least find a way to get it to accept parameters?

All help is apprecated :wink:
 
Last edited:
Level 7
Joined
Nov 12, 2005
Messages
299
Its impossible to make it take a value - timer callbacks can only be functions that both take and return nothing.
The problem is very easy to solve, however. All you need is Local Handle Vars or the CSCache. Personally I recommend the second system, but might be harder to implement and generally has a lot of functions you wont need if you only intend to do this. Well I'll give you the example for the use of both and you decide yourself.
To implement the Local Handle Vars just copy its code to your map header. To do this click on your map's name at the top of the tree in the trigger editor. Also you need to create a variable of type Game Cache called gcache for example and replace the function
JASS:
function LocalVars takes nothing returns gamecache
    // Replace InitGameCache("jasslocalvars.w3v") with a global variable!!
    return InitGameCache("jasslocalvars.w3v")
endfunction
with
JASS:
function LocalVars takes nothing returns gamecache
    if (udg_gcache == null) then
        set udg_gcache = InitGameCache("jasslocalvars.w3v")
    endif
    return udg_gcache
endfunction
And here's an example that uses handle vars. CreateUnit(...) is not a valid function btw, its just to show you that the unit is local - created every time the function fires.
JASS:
function ShowLocalUnitName takes nothing returns nothing
    local unit u = GetHandleUnit(GetExpiredTimer(), "un")
    call BJDebugMsg(GetUnitName(u))
    set u = null
endfunction

function MainActions takes nothing returns nothing
    local timer t = CreateTimer()
    local unit u = CreateUnit(...)
    call SetHandleHandle(t, "un", u)
    call TimerStart(t, 1, true, function ShowLocalUnitName)
    call PolledWait(10)
    call DestroyTimer(t)
    set u = null
endfunction
What this does? It "attaches" the unit to the timer using Local Handle Vars so that it can be used locally. This trigger is MUI therefore.
Notably this leaks - the timer should be set to null as well, but this will cause the handle to bug up and the unit might not get moved to the second function. However the leak is minor and you don't really need to worry about it...but the CSSafety (a part of the CSCache) allows you to solve this.
Same trigger, but done with CSCache and CSSafety.
JASS:
function ShowLocalUnitName takes nothing returns nothing
    local unit u = GetAttachedUnit(GetExpiredTimer(), "un")
    call BJDebugMsg(GetUnitName(u))
    set u = null
endfunction

function MainActions takes nothing returns nothing
    local timer t = NewTimer()
    local unit u = CreateUnit(...)
    call SetHandleHandle(t, "un", u)
    call AttachObject(t, 1, true, function ShowLocalUnitName)
    call PolledWait(10)
    call ReleaseTimer(t)
    set u = null
endfunction
NewTimer creates the timer if one doesn't exist already and stored it in a cache. Once you use ReleaseTimer the timer is dumped (but not destroyed) and can be used again by NewTimer next time.

If you need anything above explained better (I'm aware I don't really do great in that part) feel free to ask.
 
Status
Not open for further replies.
Top