• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

[vJASS] ExecuteFunc

Status
Not open for further replies.
JASS:
function HelloMyNameIsEd takes nothing returns nothing
    call DestroyTrigger(CreateTrigger())
endfunction

private struct Inits extends array
    private static method onInit takes nothing returns nothing
        call ExecuteFunc("HelloMyNameIsEd")
    endmethod
endstruct

That's how it works :3

The difference between this and call is that this is more dynamic and it opens up a new thread. (Well, not exactly opening up a new thread, but it simulates it. Warcraft III is single-threaded)

Dynamism:

JASS:
globals
    private string func = ""
endglobals

// ....

call ExecuteFunc(func)

// ...

set func = "..."

That way, you could change the function that gets executed.
 
Level 7
Joined
Apr 30, 2011
Messages
359
here:
JASS:
// instead of doing this:
call TriggerEvaluate(trigger) // this isn't dynamic, let's see the process

local trigger t = CreateTrigger()
call TriggerRegisterCondition(t, Condition(function toCall)) // or
call TriggerRegisterAction(t, Condition(function toCall))
// execute it
call TriggerEvaluate(trigger)

// do this instead
call ExecuteFunc("toCallName") // this is more flexible, due the use of string,
                               // and take a look at the process

local string f = "MyFunction" // optional
call ExecuteFunc(f) // static use

// dynamic use
local integer i = 5
loop
    exitwhen i == 0
    call ExecuteFunc("MyFunction" + I2S(i))
    set i = i - 1
endloop
 
Level 7
Joined
Apr 30, 2011
Messages
359
you don't get it ~.~ . . . . .
the main function is to run the function, ignoring the arguments . . .
same goes to that TriggerEvaluation method i listed above . .

here another one:
JASS:
// you will get sucked up using these things
if FunctionName == "ABC" then
    call ABC(arg1, arg2, arg3)
elseif FunctionName == "CDE" then
    call CDE(arg1, arg2, arg3)
// . . . . so on

// this is a short alternative, easy to use, but missing arguments
// arguments aren't that necessary, except for APIs and natives
call TriggerEvaluate(trigger) // common ways
call ExecuteFunc("string") // rare ways, due to the ineffectiveness of .name thing
// i'm not quite experienced with these things
call function.evaluate()
call function.execute()

edit:
sorry, i misunderstood D:
it will work just fine ~
now i don't know ~
maybe not . . .
 
Last edited:
Level 13
Joined
Sep 13, 2010
Messages
550
ExecuteFunc is not vJass, it is only used to call "onInit" and initializer functions. The called function don't have to exist, so you can set anything there just nothing will happen and thread( function/trigger ) will continue. ExecuteFunc also ignores function priority order:

JASS:
function A takes nothing returns nothing
     call ExecuteFunc( "B" )
endfunction

function B takes nothing returns nothing
     // If function A is called then this will run
endfunction
Also it runs a new thread, so function "B" will start operation count at 0.

JASS:
function A takes nothing returns nothing
     call ExecuteFunc( "A" )
     // Infinite loop, never ends cause op limit never be reached also crashes warcraft
endfunction
And if you try to execute a function that takes arguments warcraft will crash at load. Thats why onInit functions must be static.
 
There are several main ways to run code:

JASS:
//JASS:

call Function() //#1

call ExecuteFunc("Function") //#2

set trig = CreateTrigger()
call TriggerAddCondition(trig, Filter(function Function))
call TriggerEvaluate(trig) //#3

set trig = CreateTrigger()
call TriggerAddAction(trig, function Function)
call TriggerExecute(trig) //#4

call ForForce(bj_FORCE_PLAYER[0], function Function) //#5, unorthodox but it has its perks

call TimerStart(CreateTimer(), 60.00, true, function Function) //#6, very important

//vJass "additions":

call Function.evaluate() //When compiled, works the same as #3

call Function.execute() //When compiled, works the same as #4

Obviously, triggers are necessary for registering events, but this is to show you that you have lots of options for running code.

Keep in mind, only ExecuteFunc or TriggerAddAction lets you use TriggerSleepAction within the function. Other than that, besides calling a function, the behaviors will work the same whenever you use "ExecuteFunc" or pass the "code" type as an argument:

1) Function will open up its own thread, so if that thread crashes or reaches the op-limit the thread which existed before it will not crash.

2) The op-limit of the new thread will be reset, so you can distribute heavy actions across multiple threads when necessary.
 
It's unorthodox, and only (ab)used fairly recently, which is probably why you haven't seen it before.

Mostly you'll see its use in the JASS Resources section from Magtheridon96 and myself.

Without it, there is no way to pass a function as an argument and be able to simply "call" it. Sure with vJass you can use function interfaces and ".evaluate()" them or ".execute()" them, but interfaces double the code length which sucks.
 
Thread in the sense we are talking about is a shell containing the code you're running WHILE it's running.

That also means that if you use a TriggerSleepAction (from a Trigger action or from ExecuteFunc) that the first thread doesn't sleep during that time, so you can use it to delegate actions appropriately:

JASS:
function InitTrig_MyTrigger takes nothing returns nothing
    call ExecuteFunc("Hello")
    call BJDebugMsg("Hello")
endfunction

function Hello takes nothing returns nothing
    call TriggerSleepAction(0)
    call BJDebugMsg("World")
endfunction

//This will display to you:

"Hello"
...
"World"
 
Level 13
Joined
Sep 13, 2010
Messages
550
Thread in the sense we are talking about is a shell containing the code you're running WHILE it's running.

That also means that if you use a TriggerSleepAction (from a Trigger action or from ExecuteFunc) that the first thread doesn't sleep during that time, so you can use it to delegate actions appropriately:

JASS:
function InitTrig_MyTrigger takes nothing returns nothing
    call ExecuteFunc("Hello")
    call BJDebugMsg("Hello")
endfunction

function Hello takes nothing returns nothing
    call TriggerSleepAction(0)
    call BJDebugMsg("World")
endfunction

//This will display to you:

"Hello"
...
"World"


Wait won't work
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Imagine that you want to run a function out of one hundred depending on the content of a variable.

So you could write:
JASS:
if (var == 1) then
    call Func1()
elseif (var == 2) then
    call Func2()
elseif (var == 3) then
    call Func3()
elseif (var == 4) then
    call Func4()
...
elseif (var == 100) then
    call Func100()
endif

You see this is inconvenient, produces lots of code, is kind of slow if var is high and these functions might originate from some completely other position that has nothing to do with it.

Instead you could write:
JASS:
call ExecuteFunc("Func" + I2S(var))

or you do not even know how the function is named, it's just passed as a code parameter or in another container.

So you can do this for example:
JASS:
local trigger t = CreateTrigger()

call TriggerAddCondition(t, Condition(code))

call TriggerEvaluate(t)
 
Well if something is vague make a post about it, like you have done here. I recommend if the thing is too vague, skip over it for now and read the things that make sense, and then tomorrow when your brain has settled from the new material you can try again to grasp it.

The best ways to learn: write out the code yourself, and see how it compiles (by checking the outputwar3map.j file in the logs folder of the JNGP folder).
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,545
vJass can do a lot of things, but not all of them are really THAT helpful.

Your best bet is to understand the important, useful stuff, and use it.

Here are some good keywords to understand from the top of my head:
JASS:
scope
library
struct
method
globals
private
constant
public

Other things like text macros are quite unnecessary for now.

Good luck and happy coding!
 
Status
Not open for further replies.
Top