- Joined
- Feb 27, 2007
- Messages
- 5,578
Has anyone else run into a situation in which you needed a way to hook into a function to get its output rather than the inputs given to it? Like the hook keyword but in reverse. When hooks are used they are called before the function itself and do not provide you with a way to get its return arguments. By way of example:
This behavior is fine, (somewhat) expected, and useful in many situations. But what about if you want to hook something like CreateUnit so that you can do some actions with the unit after it's created? With the way hooks work now, this is impossible because you can't get the return value of the function. I propose something like:
Why would this be useful because you should theoretically always have access to the return value of a function you called, right? Besides something like the CreateUnit scenario I mentioned before where you might want to set mapwide functions to run whenever a unit is created (from any source in your map) the scenario that got me thinking about this is trying to hook into CreateTrigger to do something with it whenever it's called. Since the function has no arguments hooking immediately seemed pointless since I can't actually access the trigger object that was created by the function! Sure I could write a CreateTriggerEx that called CreateTrigger itself and did whatever I needed to do, but implementing that in a map would require changing all CreateTrigger calls to use the Ex version--could be a lengthy process to find/replace all your code (I guess not with editing the war3map.j file though).
Thoughts?
JASS:
function FuncA takes nothing returns nothing
call BJDebugMsg("A")
endfunction
function FuncB takes nothing returns nothing
call BJDebugMsg("B")
endfunction
hook FuncA FuncB //B is hooked when A is called
//...
call FuncA() //The output is B,A not A,B
JASS:
function FuncA nothing returns integer
call BJDebugMsg("Random math:")
return GetRandomInt(1,3)
endfunction
function FuncB takes integer RET_VALUE returns nothing
call BJDebugMsg(I2S(RET_VALUE)+"times 5 is "+I2S(RET_VALUE * 5))
endfunction
hookreturn FuncA FuncB //A's output is hooked into B after it's
//...
call FuncA() //the output is "Random math: _ times 5 is _"
Thoughts?