WC3Jass.com Forum Index WC3Jass.com
"The Jass Vault"
 
 FAQ   Search   Memberlist   Usergroups   Register 
 Profile   Log in to check your private messages   Log in  
 Forum   Scripts   Files   Chat   Pastebin   Function finder  
Affiliates
The HIVE Workshop Games Modding
The Hubb Wc3Campaigns
WC3-Mapping.net

ExecuteFunc: How to use
Goto page 1, 2  Next
 
Post new topic   Reply to topic    WC3Jass.com Forum Index -> Tutorials
View previous script :: View next script  

Author
Blade.dk


Joined: 20 Apr 2005
Posts: 608
Back to top
Message
PostPosted: Thu Oct 13, 2005 2:43 pm    Post subject: ExecuteFunc: How to use Reply with quote

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:

Code:
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:

Code:
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:

Code:
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
_________________
#wc3dev


Last edited by Blade.dk on Sun Jan 08, 2006 6:48 pm; edited 3 times in total
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

Author
Ryude


Joined: 13 Oct 2005
Posts: 8
Back to top
Message
PostPosted: Thu Oct 13, 2005 2:50 pm    Post subject: Reply with quote

That is very useful indeed, I'll have to try it sometime.
View user's profile Send private message

Author
Blade.dk


Joined: 20 Apr 2005
Posts: 608
Back to top
Message
PostPosted: Sun Jan 08, 2006 6:47 pm    Post subject: Reply with quote

Edited to fix some errors.
_________________
#wc3dev
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

Author
Promise


Joined: 16 Jan 2006
Posts: 14
Back to top
Message
PostPosted: Mon Jan 16, 2006 6:09 am    Post subject: Reply with quote

Thank you !You make a chinese learn a lot!
View user's profile Send private message

Author
lookoverthere
Guest



Back to top
Message
PostPosted: Fri Feb 10, 2006 3:57 am    Post subject: Reply with quote

Bah, I did not know executefunc could use forward references.
Only problem is cant use handle functions with this, right?

Author
Edwardo
Guest



Back to top
Message
PostPosted: Thu Mar 23, 2006 1:07 am    Post subject: Reply with quote

Ive not found one use where exc funct is usefull. You cant carry any value through handle vars.

Author
Blackroot


Joined: 07 Mar 2006
Posts: 135
Back to top
Message
PostPosted: Thu Mar 23, 2006 2:19 am    Post subject: Reply with quote

I use it loads, I dont know what you're talking about. You can use handle vars to pass things to it.

Code:

function a1 takes nothing returns nothing
 local something h = GetHandleHandle(GetTriggeringTrigger(), "A Thing")
endfunction

// ...

call SetHandleHandle(GetTriggeringTrigger(), "A Thing", handle)
call ExecuteFunc("a1")



I've used this method alot in my triggers, they work perfectly.
View user's profile Send private message

Author
Blade.dk


Joined: 20 Apr 2005
Posts: 608
Back to top
Message
PostPosted: Thu Mar 23, 2006 6:25 pm    Post subject: Reply with quote

Or use globals temporarily.
_________________
#wc3dev
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

Author
Edwardswolentoe


Joined: 29 Jul 2005
Posts: 144
Back to top
Message
PostPosted: Fri Mar 24, 2006 11:53 am    Post subject: Reply with quote

Oh so the function called by exc still retains the get triggering trigger. This is an intreguing development.
View user's profile Send private message Send e-mail MSN Messenger

Author
GX-43AH


Joined: 21 Nov 2006
Posts: 8
Back to top
Message
PostPosted: Wed Nov 22, 2006 9:41 am    Post subject: Reply with quote

But, there are something need to be adverted.
If the distance between the function which is called and the main function is to far,it wouldn't work.
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger

Author
Blade.dk


Joined: 20 Apr 2005
Posts: 608
Back to top
Message
PostPosted: Sun Nov 26, 2006 8:45 pm    Post subject: Reply with quote

Yes it will.

Sorry for not posting it here, but it was caused by a problem (that was never solved or figured out) in the old test map, so distance is no problem.
_________________
#wc3dev
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

Author
PurplePoot


Joined: 20 Dec 2005
Posts: 272
Back to top
Message
PostPosted: Wed Nov 29, 2006 1:39 am    Post subject: Reply with quote

Just outta curiosity...

I noticed that you use GetEnumUnit() in one of your examples to allow waits in groups.

A) Does that mean that functions like GetEnumUnit() and GetTriggerUnit() are passed through when creating a new thread?

B) Can you pass local variables in some manner or other as well?

Also, another totally unrelated question

If i had function ABC, and

Code:
function ABD takes nothing returns nothing
    call ExecuteFunc( "A" + "B" + "C" )
endfunction


Would that crash it?
View user's profile Send private message

Author
Mapz_Maker (not logged in
Guest



Back to top
Message
PostPosted: Wed Dec 20, 2006 6:03 pm    Post subject: Reply with quote

Actually i think ExecuteFunc can take something, look at this line of code found in a fullscreen inventory system that works really well.
Code:
   call ExecuteFunc(get_session_param("fs_module") + "_ClickHandler")

There are no compile errors or anything, the system does work, it must be possible.

Author
KaTTaNa
Site Admin

Joined: 04 Apr 2005
Posts: 655
Back to top
Message
PostPosted: Wed Dec 20, 2006 7:16 pm    Post subject: Reply with quote

You can create the string however you want, but the function you are calling cannot take parameters.

For example, if you tried to do this, the game would crash:
Code:

function Something takes integer i returns nothing
    // ...
endfunction

function SomethingElse takes nothing returns nothing
    // ...
endfunction

 ...

call ExecuteFunc( "Something(45)" ) // Does not work!
call ExecuteFunc( "Something" ) // Does not work!
call ExecuteFunc( "SomethingElse" ) // Works!



In the fullscreen inventory system, there is simply a function called FSInventory_ClickHandler or something like that. Whatever it is called it takes no parameters.
View user's profile Send private message Send e-mail Visit poster's website

Author
Mapz_Maker


Joined: 25 Nov 2006
Posts: 178
Back to top
Message
PostPosted: Wed Dec 20, 2006 9:07 pm    Post subject: Reply with quote

call ExecuteFunc(get_session_param("fs_module") + "_ClickHandler")

you see i layered the line of code, blue = inside 0 parenthesis, green = inside one parenthesis (the parameter for ExecuteFunc), orange = inside two parenthesis (parameters for get_session_param).

although my tests to get multi-layered ExecuteFunc have failed this works

view the inventory system here Inv100_Release.w3x

the executefunc use is found in function fs_ClickHandler under FSGUI Main Script.


Last edited by Mapz_Maker on Wed Dec 20, 2006 9:30 pm; edited 1 time in total
View user's profile Send private message

Display posts from previous:   
Post new topic   Reply to topic    WC3Jass.com Forum Index -> Tutorials All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


FSDark by SkaidonDesigns
Powered by phpBB © 2001, 2002 phpBB Group