Wc3C.net (http://www.wc3c.net/forums.php)
-   JASS/AI scripts tutorials (http://www.wc3c.net/forumdisplay.php?f=650)
-   -   How to use the ExecuteFunc native (http://www.wc3c.net/showthread.php?t=79029)

Blade.dk 01-08-2006 06:50 PM

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")

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

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


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."

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

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

    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

masda70 04-15-2006 04:32 AM

I just came across this tutorial and I was surprised to read that TriggerSleepAction halts ForGroup callback. I've been using that all the time and also just tested it now, it works just fine!

PipeDream 04-15-2006 05:29 AM

I just tested as well. Seems to still not be working though... Post your test, Masda?

masda70 04-16-2006 07:56 PM

Hmm nevermind, I thought this meant a for group callback was a single thread that would run each wait one after another thus delaying the whole execution of every unit to go one after the other hehe, I see the problem is different. Great info then!

BertTheJasser 04-19-2006 07:52 PM

He starts
Collapse JASS:
every time in an extracted single thread. This alows him to use TriggerSleepAction or PolledWait or any custom wait method.

Great tut.

All times are GMT. The time now is 04:26 AM.

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services