HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

function/native hooks

05-21-2009, 02:43 PM#1
Vexorian
Yeah that's right, once again I need syntax for something...

Idea: you somehow declare a "function usage event" then it ... works:


Collapse JASS:
hook RemoveUnit RemoveUnitEx

function meh takes unit u, unit v returns nothing
    call RemoveUnit(u)
endfunction


// much Later in the map
hook RemoveUnit onRemoveUnit




becomes:

Collapse JASS:
// just after the globals section:

function h__RemoveUnit takes unit u returns nothing
    call RemoveUnitEx.evaluate(u)
    call onRemoveUnit.evaluate(u)
    call RemoveUnit(u)
endfunction

//whenever meh was declared:

function meh takes unit u, unit v returns nothing
    call h__RemoveUnit(u)
endfunction




It wouldn't initially work on the map's functions until I add a way for the user to determine if the function allows hooks. And yes, ".evaluate is slow pizda".
05-21-2009, 04:02 PM#2
grim001
Looks perfect to me, I like the simple syntax of "hook function1 function2". I think making it work on non-natives is not very important at the moment. I can't think of an instance where it makes sense to use it that way.
05-21-2009, 04:57 PM#3
MaD[Lion]
this is awesome, was one of the requests i made in jasshelper, dunno if u read it, but im glad ure making it :)

The syntax looks really fine to me, but will u be able to hook a user defined function to others? Or are the RemoveUnit auto generated.

example:

Collapse JASS:
hook FunctionA FunctionB
hook FunctionA FunctionC
...
function FunctionA takes nothing returns nothing
    call BJDebugMsg("Among the hooks")
    //...
endfunction
...
function teh takes nothing returns nothing
    call FunctionA()
endfunction

wat will happen here?
05-21-2009, 05:05 PM#4
Alevice
Does this override stuff in blizzard.j? CustomRcaeSystem would surely benefit from shortening the code.
05-21-2009, 10:12 PM#5
Toadcop
Quote:
It wouldn't initially work on the map's functions until I add a way for the user to determine if the function allows hooks. And yes, ".evaluate is slow pizda".
Just Another Epic Quote from Vex <3
05-21-2009, 10:36 PM#6
Silvenon
Quote:
".evaluate is slow pizda".

Hahaha, yeah, we have the same word for that in croatian :D

I don't get this hook thing :(
05-22-2009, 04:21 AM#7
grim001
Quote:
Originally Posted by Silvenon
I don't get this hook thing

There's a lot of stuff that you can't detect before it happens in WC3, like RemoveUnit. This lets any system detect it without modifying the rest of the map's code.
05-22-2009, 01:21 PM#8
Silvenon
Quote:
Originally Posted by grim001
There's a lot of stuff that you can't detect before it happens in WC3, like RemoveUnit. This lets any system detect it without modifying the rest of the map's code.

Thanks, I think I get it now.
05-22-2009, 02:56 PM#9
Anitarf
Would this finally fully allow optional code? Right now any kind of optional module that requires some sort of init when an object is created can't be done without the user having to edit the code of the system itself depending on whether they want to have the optional module or not. With this, you could maybe hook stuff up to create methods so that system X can work with or without module Y without the user needing to do anything?
05-22-2009, 03:34 PM#10
grim001
Yeah, that would be an interesting application of it once it works with non-natives.
05-22-2009, 05:03 PM#11
Ammorth
I was looking for way to setup optional libraries that would modify a generic library, while keeping all the functions the same. This (if setup to use with user-created functions) would solve just that.
05-22-2009, 08:23 PM#12
darkwulfv
I don't understand what this is supposed to even do. Grim's explanation really didn't make much sense to me.
05-22-2009, 08:36 PM#13
akolyt0r
Quote:
Originally Posted by darkwulfv
I don't understand what this is supposed to even do. Grim's explanation really didn't make much sense to me.

its useful, if you need to execute some functions everytime a specific native is called ...
for example unregister units with some systems every time RemoveUnit is called ...
05-22-2009, 08:42 PM#14
darkwulfv
Ahh, all right. Now if only I understood how to use it. That's a pretty cool new feature :D
05-22-2009, 08:57 PM#15
rain9441
Collapse JASS:
observer RemoveUnit

function meh takes unit u, unit v returns nothing
    call RemoveUnit(u)
endfunction

function mehInit takes nothing returns nothing
    subscribe RemoveUnit.Post, blarg 
    // This really should create an onInit function instead of be in one
endfunction

function blarg takes unit u returns nothing
    call BJDebugMsg("unit was just removed")
endfunction

// 3000 lines later

observer RemoveUnit

becomes:

Collapse JASS:
// just after the globals section:

function h__RemoveUnit takes unit u returns nothing
    call NativeEventOnPreRemoveUnit.evaluate(u)
    call RemoveUnit(u)
    call NativeEventOnPostRemoveUnit.evaluate(u)
endfunction

//whenever meh was declared:

function mehInit takes nothing returns nothing
    call TriggerAddCondition(NativeEventOnPreRemoveUnit, SomeDeepCompiledJassStuffThatHandlesVariablesAndSuchThatVexWouldHaveToFigureOut)
endfunction

function meh takes unit u, unit v returns nothing
    call h__RemoveUnit(u)
endfunction

Or more indepth...
Collapse JASS:
library X
    observer SuperSweet

    function SuperSweet takes integer b returns nothing
    endfunction

endlibrary

library Y
    subscribe X.SuperSweet.Pre BeforeSuperSweetDoMe
    subscribe X.SuperSweet.Post AfterSuperSweetDoMe

    function BeforeSuperSweetDoMe takes integer x returns nothing
    endfunction
    function AfterSuperSweetDoMe takes integer x returns nothing
    endfunction
endlibrary

Does that make any sense at all? I don't think it would be possible on a per object basis though.

EDIT: I personally had no use for this feature because I find it very easy to just use RemoveUnitEx type functions, but if this were able to be added in one line of code and hooked up to a function it would open up amazing amounts of opportunity for debugging, automated statistic tracking, etc...