Function pointers

Status
Not open for further replies.
Level 12
Joined
May 22, 2015
Messages
1,051
Is there a way to pass functions to other functions? Is there a way to store them in variables?

What I've been doing instead is creating triggers and using those since it is ALMOST exactly the same thing. I just figure actual function pointers would be faster than checking trigger conditions since it doesn't require creating new objects and potentially deleting them.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
Is there a way to pass functions to other functions?
Yes, with trigger or string.

Is there a way to store them in variables?
Yes, as a trigger or string.

What I've been doing instead is creating triggers and using those since it is ALMOST exactly the same thing. I just figure actual function pointers would be faster than checking trigger conditions since it doesn't require creating new objects and potentially deleting them.
There are no real function pointers. The trigger approach is generally the best approach and I believe it is how vJASS implements function references.

Do remember that since it is a reference to constant code, you only need to create the trigger once and you can then use it as a constant. This makes them more efficient as you can create all the function reference triggers at map initialization and then use them as constant arguments.

You can even pass them arguments by using global variables in a way similar to registers. You place the arguments in the variable, then at the start of the referenced function you copy the arguments into local variables. This even supports recursive calls.

Strings can be used as an alternative to triggers since you can run functions by name with a native. In some ways this is easier since it forgoes trigger creation and binding but it comes at the cost of being slower (or so people say). It also is not optimizer friendly as obfuscation can some times (not always) mess up the strings or functions. Arguments are passed in the same way as with triggers, by using register like globals and then copying to locals.
 
Level 12
Joined
May 22, 2015
Messages
1,051
What type do you need? How do you want to call them? Or do you only need to distinguish them? Are we talking about functions that take no parameters or otherwise?

Parameters is best, but I can usually manage with global variables.

JASS:
function ExecuteFunction takes code code returns nothing
    call ForForce(DUMMY_FORCE, code)
endfunction
Just make a player group and make sure there is only one player inside.
Then this will work... but you still cant make code arrays.

That's a crazy way to do it lol.

Is it ever better than just using triggers?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
JASS has no native function pointer support which you can use outside of natives. SC2 does, but that is not available in GUI.

vJASS implements function pointers using triggers and global variables to pass arguments. If you want to use them in a maintainable way then I would recommend using the vJASS ones instead of hacking in your own implementation.
 
Status
Not open for further replies.
Top