View Single Post
Old 01-08-2006, 06:50 PM   #1
Blade.dk
.
 
Blade.dk's Avatar


Respected User
 
Join Date: May 2005
Posts: 1,990

Submissions (15)

Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)

Approved Map: Azeroth's Arcane ArenaSpell session 01 winner

Send a message via MSN to Blade.dk
Default How to use the ExecuteFunc native

As lots of people don't know the ExecuteFunc native, here is a brief tutorial about how to use it.

NOTE: This tutorial requires you to know, at least, the basics of JASS. If you don't know it, search the web for tutorials focused on that, I'd recommend Vexorian's Introduction to JASS.

Table of contents
1. What is ExecuteFunc?
2. Which functions can I use with it?
3. How exactly do I call it?
4. Give me some examples!
5. Are there any problems with ExecuteFunc?

1. What is ExecuteFunc?

ExecuteFunc is just a native, but one of the most useful natives.

It takes a function's name as a string argument, and executes the function in another thread.

This means, that the executed function can use waits without interrupting the calling thread, and as it will remember event responses, it allows you to use waits in for example ForGroup and TimerStart calls.
Moreover, it's the only way to have forward references in JASS.

2. Which functions can I use with it?

You can use ExecuteFunc with any function that takes nothing, the function may be constant and is allowed to return a value, even though you won't be able to access that value, unless the function stores it in a global.

As said above, the ExecuteFunc native allows forward references, so the function may be below the call in the map's script file.

3. How exactly do I call it?

Calling ExecuteFunc is simple when you know JASS. You just call it with the function's name as a string argument to execute the function.

The funcName argument is case sensitive and has to be spelled correctly; else it'll cause crashes.
You can use things like concatenation, just be sure that the string it ends up with is valid.

It won't cause crashes if you use null or "" as argument.

You can make it execute the function it is called from within, but be sure you prevent it from turning into an infinite loop without pauses, as that will cause the map to crash.

4. Give me some examples!

Basic example of calling it:

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

function Hi takes nothing returns nothing
    call BJDebugMsg("Hi")
endfunction

When the Hello function is called, this will display the text:

Hi
Hello

The reason "Hi" is displayed before "Hello" is that the ExecuteFunc call in the Hello function is located over the BJDebugMsg call. This is also an example of a forward reference using ExecuteFunc.

You could have replaced the Hi function with the following function without problems:

Collapse JASS:
constant function Hi takes nothing returns string
    return "This is idiotic, but it's still possible."
endfunction

Before I talked about having TriggerSleepAction calls in a ForGroup callback function. Example:

Collapse JASS:
function Enum2 takes nothing returns nothing
    local unit e = GetEnumUnit() // Store the enumerated unit in a local variable
    call TriggerSleepAction(5) // Wait five seconds
    call KillUnit(e) // Kill the unit
    set e = null // Set the local to null to avoid memory leaks
endfunction

function Enum takes nothing returns nothing
    call ExecuteFunc("Enum2") // Let's execute the Enum2 function to allow waits.
endfunction

...
    call ForGroup(someGroup, function Enum) // will kill all units in the 'someGroup' group after five real-time seconds.
...

Normally a TriggerSleepAction call would halt the ForGroup call, but not here, as the waits are in the Enum2 function that is executed with ExecuteFunc.

It also shows that event responses, like GetEnumUnit, works completely fine with ExecuteFunc.

5. Are there any problems with ExecuteFunc?

Yes. It will crash your map if the funcName argument is not a valid function name, but this won't happen if the user, you, know what he does.

Another problem occur when you use map protectors like Heavylocker's or Extprotect's function obfuscating method that renames functions to random things. As they don't change the contents of the strings having the same name as functions, it will cause crashes when it's called.

The solution would be using Vexorian's Map Optimizer, as long as you just use the strings directly (without using concatenation or things like that). The other solution would be to avoid using those tools, or at least avoid using those obfuscation methods.

I hope this helped somebody.
- Blade.dk
__________________
Spell Making Course: Part 1: Making a simple stomp spell.
I wonder if I'll ever finish part 2.
Blade.dk is offline   Reply With Quote
Sponsored Links - Login to hide this ad!