[JASS] Two functions calling each other...

Status
Not open for further replies.
Level 8
Joined
Jul 23, 2005
Messages
329
I tried searching, but had no idea which keywords would produce the following question without having 3 pages of posts that don't have anything to do with the question I was asking.

...So here's to a new topic!

I have two functions calling each other, one which calls the other via a timer, the other which calls the other via "call", but passes on a unit variable. So... The code looks like this:

JASS:
function WaitForTheAttack takes nothing returns nothing //Called by timer
    //Does stuff with the timer, and...
    call BounceyBouncey( Attacked )
endfunction

function BounceyBouncey takes unit BounceFrom returns nothing
    //Does stuff with the unit, and...
    set TempTimer = CreateTimer()
    call TimerStart( TempTimer, 0.01, true, function WaitForTheAttack ) )    
endfunction

And now I encounter a problem. I can't do this because the function BounceyBoucey doesn't exist yet when the compiler gets to the "Call BounceyBoucey" line, so yeah. Any way I can get a function to call on a function below it?
 
Level 8
Joined
Jul 23, 2005
Messages
329
there is no need to do that in jass.. it would cause an infinite loop
I was planning to control the amount of repitions by an integer variable.

@Hindy, I've heard of both those functions, but I've never thought about using globals. The problem is, this will stop it from become MUI. Any thoughts around this?
 
Level 20
Joined
Apr 22, 2007
Messages
1,960
Won't stop it from being MUI at all. You can set the global to a local at the beginning of the executed function:
JASS:
function Executed takes nothing returns nothing
    local unit u=bj_ghoul[100]
    call TriggerSleepAction(30.)
    call RemoveUnit(u)
    set u=null
endfunction
function Something takes nothing returns nothing
    set bj_ghoul[100]=GetTriggerUnit()
    call ExecuteFunc("Executed)
endfunction
 
Level 1
Joined
Jan 24, 2008
Messages
5
how can you call bounceythingy when it is under that function, doesnt jass work from top to bottom?
 
Level 11
Joined
Feb 18, 2004
Messages
394
how can you call bounceythingy when it is under that function, doesnt jass work from top to bottom?

The "call" statement works top-to-bottom. Thusly, you cannot have mutual recursion (two functions which call eachother) by simply using the "call" statement. Eg, this will not work:
JASS:
function A takes nothing returns nothing
    call B()
endfunction

function B takes nothing returns nothing
    call A()
endfunction

However, there are methods of invoking a function which do not use the "call" statement. An example of which is ExecuteFunc(), which is a function that takes a function name as an argument, and then executes it. Thusly, this WILL work:
JASS:
function A takes nothing returns nothing
    call ExecuteFunc("B")
endfunction

function B takes nothing returns nothing
    call A()
endfunction

There are other methods of calling a function which allow you to break the "Top-to-Bottom" rule, such as creating a trigger, adding the function you wish to execute as an action to the trigger, then using TriggerEvaluate() or TriggerExecute() on the trigger.


As for mutual recursion the easy way, download the JASS NewGen pack (link in my sig) and use vJASS's .evaluate() and .execute() constructs.

From the JASS Helper (vJASS) Manual:
JASS:
function A takes real x returns real
 if(GetRandomInt(0,1)==0) then
    return B.evaluate(x*0.02)
 endif
 return x
endfunction

function B takes real x returns real
 if(GetRandomInt(0,1)==1) then
    return A(x*1000.)
 endif
 return x
endfunction
http://wc3campaigns.net/vexorian/jasshelpermanual.html#funcobj
 
Status
Not open for further replies.
Top