• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Jass call dynamic functions

Status
Not open for further replies.
Level 3
Joined
Jul 1, 2010
Messages
31
Hello,
is it possible to call a function dynamically?

JASS:
globals
	integer udg_MyValue = 0
endglobals

function FirstFunction takes nothing returns nothing
	set udg_MyValue = 1
endfunction

function SecondFunction takes nothing returns nothing
	set udg_MyValue = 2
endfunction

// Im searching something like that:
function FunctionThatISearchFor takes function f returns nothing
	call RunFunction(f)
endfunction

function InitTrig_Stuff takes nothing returns nothing
	call FunctionThatISearchFor(FirstFunction)
endfunction
 
Last edited:
Level 19
Joined
Mar 18, 2012
Messages
1,716
Yes you could do that.
1. executeFunc("string") the string represents the function name. Note that executeFunc is very slow.
2. TriggerEvaluate(trigger). You can do TriggerAddCondition(trigger, function). Fire the trigger and remove the condition afterwards.

What kind of resource are you trying to create?
 
Yes you could do that.
1. executeFunc("string") the string represents the function name. Note that executeFunc is very slow.
2. TriggerEvaluate(trigger). You can do TriggerAddCondition(trigger, function). Fire the trigger and remove the condition afterwards.

What kind of resource are you trying to create?

ExecuteFunc isn't that slow, and it's surely faster than TriggerEvaluate.
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
I ran some tests on TriggerEvaluate and I guess it's a bit faster than ExecuteFunc.
That is why vJASS uses it for its generics. Also ExecuteFunc used to fatal error in the case of an invalid string making it prone to programming errors although that has been fixed now.

I guess for dynamically running code ExecuteFunc would be a bad choice considering there's no C2S function, and the code type isn't a handle.
Actually its convenient since you can just pass strings around. A string can then represent dynamic code. Pretty cool and easy to use. The only problem is the lack of syntax support so you can rename a function and forget to update the string (no syntax error generated like with function calls in such a case). Also the main problem is that Vexorian Optimizer performs a function name shortening step which can cause problems as it does not have perfect logic for handling ExecuteFunc (last I knew it would try and leave such function names alone but there have been cases where this logic is not perfect.

As such people generally use triggers since they are completely immune to the effects of the optimizer.
 
is the performance of ExecuteFunc slower also than the overhead of trigger evaluation (create trigger, add condtion, evaluate, destroy trigger, null)?

ExecuteFunc was about 30% faster. Check the benchmarking thread I've updated it.

JASS:
    //! textmacro STOPWATCH_BENCHMARK_TEST_1
        call ExecuteFunc("A")
    //! endtextmacro 
    
    //! textmacro STOPWATCH_BENCHMARK_TEST_2
        set T=CreateTrigger()
        call TriggerAddCondition(T, Filter(function B))
        call TriggerEvaluate(T)
        call DestroyTrigger(T)
        set T=null
    //! endtextmacro

3.jpg
 
Status
Not open for further replies.
Top