Functions calling themselves, +rep for answers

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
Example:
JASS:
function Example takes nothing returns nothing
    if Bool then
        set Bool = false
        call Example()
    endif
endfunction


would this compile as the code itself and simply call itself, or would it compile making a copy of itself above the function and call that instead?
 

luorax

Invasion in Duskwood
Reaction score
67
No, it would pop up an error saying JASS doesn't support recursion. However it might work if you use [ljass]ExecuteFunc[/ljass]; I can't remember exactly.
 

lep

Active Member
Reaction score
8
No, it would pop up an error saying JASS doesn't support recursion. However it might work if you use [ljass]ExecuteFunc[/ljass]; I can't remember exactly.

Nah, Jass supports recursion. So the above will work.
It does not support calling a function which was not defined before calling, so smth like this wont work:
JASS:
function even takes integer i returns boolean
    if i == 0 then
        return true
    else
        return odd(i-1) 
    endif
endfunction

function odd takes integer i returns boolean
    if i == 0 then
        return false
    else
        return even(i-1)
    endif
endfunction


But with vJass we can make it work:
JASS:
// untested
function even takes integer i returns boolean
    if i == 0 then
        return true
    else
        return odd.evaluate(i-1)
    endif
endfunction

// def. of odd from above
 

luorax

Invasion in Duskwood
Reaction score
67
I know that you can't call functions above the current one, but as I remembered recursive functions weren't supported either; the memories of some old JH errors confused me.

I don't recommend using vJASS's evaluate/execute; code properly or use [ljass]ExecuteFunc[/ljass]. Those calls add too much useless code to your map script.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
but using [ljass]ExecuteFunc[/ljass] is slow and can cause crashes if you use it wrong, also... TriggerEvaluate is simply faster and wont cause crashes, the only + i see in [ljass]ExecuteFunc[/ljass] is the use to store pseudo-code in strings and execute it like that allowing for "code" arrays
 

Dirac

22710180
Reaction score
147
Jass allows recursion.
Proof

EDIT:
It's not as easy as it sounds GFreak, remember that functions inside scopes compile with a different name, and excecute func takes the actual name of the function, so a string array wont work.
Instead of code arrays use boolexpr arrays
If you wish to fire the boolexpr array add it as a condition to a trigger and fire it.
Works even better if you plan to fire many at the same time (add them all as conditions to a trigger and then fire it)
Me and Bribe also needed code arrays for my DamageDealt/TakenEvent and his Advent resources, we ended up firing trigger arrays because it allows you to know when the call ends and the next one starts (to support dynamism and data retrieval)
Also you can do this
JASS:
function StoreCode takes code c returns nothing
    call TriggerAddCondition(TRIG,Filter(c))
endfunction
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top