HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Can I freeze functions?

08-02-2006, 04:31 PM#1
Doomhammer
In my map several functions loop in the background for calculation purposes. Certain events would benefit from freezing these processes for a certain amount of time, and have them resumed at exactly the same command in the function after the event is finished.
So I'm looking basically for sth like
Code:
Freeze("function_do_sth")
UnFreeze("function_do_sth")
Is that feasible in Jass?
08-02-2006, 05:02 PM#2
Rising_Dusk
Base your functions on periodic timers in short intervals and you can just pause the timer and unpause it on command.
That's the simplest way to do it.
08-02-2006, 05:08 PM#3
TheEpigoni
You could put a loop inside the loop that only exits when a global variable is false. Then set the global to true when you want to freeze the function. Also put a triggersleepaction inside this mini-loop.
08-02-2006, 05:57 PM#4
Rising_Dusk
I don't recommend that venue (using the global/triggersleep loop) only because you can still reach the execution limit even with a triggersleep in there.
Not to mention a triggersleep doesnt make it calculate on the spot as you need it, it delays it.

I still recommend the timer pausing/unpausing bit.
Notably you'd need to make it a global timer or attach it somewhere and use cache to get it.
08-02-2006, 06:35 PM#5
The)TideHunter(
I agree with Dusk, i think a timer would suit best for this scenario.
08-02-2006, 08:34 PM#6
Doomhammer
Good answers, thanks!

Timers are ideal.

Do you know how to make use of the 'code handler Func'

Code:
native TimerStart (timer whichTimer, real timeout, boolean periodic, code handlerFunc) returns nothing
08-02-2006, 08:39 PM#7
Rising_Dusk
Collapse JASS:
function Blah_TimerStuff takes nothing returns nothing
    //Do stuff
endfunction

function Blah1 takes nothing returns nothing
    local timer tmr = CreateTimer()
    call TimerStart(tmr, 1.00, true, function Blah_TimerStuff)
endfunction

Something like that, adjusted to fit your purposes.
08-02-2006, 08:59 PM#8
Doomhammer
sure, but what's a setup like that doing?
08-02-2006, 09:04 PM#9
The)TideHunter(
Collapse JASS:
function Blah_TimerStuff takes nothing returns nothing
    //Do stuff
endfunction

function Blah1 takes nothing returns nothing
    local timer tmr = CreateTimer()
    call TimerStart(tmr, 1.00, true, function Blah_TimerStuff)
endfunction

That will create a timer in Blah1, called tmr.
It will then start it, after 1.00 secs, it will run function Blah_TimerStuff.
function Blah_TimerStuff is the 'code' part.
Code is a function pointer.
The 'true' is to check if it will keep doing it once the timer has expired, also known as periodic.
So because its true, it will run function Blah_TimerStuff every 1 second, until you pause and/or pause + destroy the timer.

EDIT: Also, its not a big deal because you'v only done it twice, but instead of wrapping your code around code tags (sounds stupid lol), wrap it around jass tags.
Old: [ code][/code]
New: [ jass][/jass]
Its got colour syntax and looks nice xD.
08-02-2006, 09:25 PM#10
Doomhammer
Quote:
Originally Posted by The)TideHunter(

So because its true, it will run function Blah_TimerStuff every 1 second, until you pause and/or pause + destroy the timer.


that's the useful part! thanks a lot!
08-03-2006, 12:07 AM#11
PipeDream
Continuations are easy to implement if you CPS transform your code and execute it in a trampoline.

Collapse JASS:
function done takes nothing returns nothing
endfunction

function mult_thunk takes nothing returns nothing
    local integer a1 = pop()
    local integer a2 = pop()
    local integer r
    set r = a1 * a2    //compiles to same as local integer r = a1*a2 so it's fine
    call push(r)
endfunction

function fact_thunk takes nothing returns nothing
    local integer x = pop()
    if x <= 1 then
        call push(1)
        call pushf(function done)
    else
        //The delayed computation (!)
        call push(x)
        call pushf(function mult_thunk)

        //Immediate call
        call push(x-1)
        call pushf(function fact_thunk)
    endif
endfunction

function trampoline takes nothing returns nothing
    local code next_func
    call push(6)    //compute fact(6)
    call pushf(function fact_thunk)

    loop
        set next_func = popf()
        exitwhen next_func == function done
        call ExecuteCode(next_func)
    endloop

    call BJDebugMsg(I2S(pop()))
endfunction
08-03-2006, 02:06 AM#12
Doomhammer
Hey Pipe!
That's very interesting...and advanced. Do you have a tutorial on the workings of 'trampoline'?
and: where do "push" and "pop" come from? Where are they defined?