WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   hooksecurefunc() with local versions of functions? (https://www.wowinterface.com/forums/showthread.php?t=57743)

LudiusMaximus 12-19-19 02:52 PM

hooksecurefunc() with local versions of functions?
 
I understood that it is considered a good habbit to declare local versions of global functions to make your code use less CPU cycles.
(See e.g. https://www.wowinterface.com/forums/...ad.php?t=57151)

So I am particularly doing

Code:

local SetCVar = _G.SetCVar
because my program uses SetCVar() a lot.

But how can I now hook a function to SetCVar()?
For _G.SetCVar I would have used:

Code:

hooksecurefunc("SetCVar", MyPostSetCVar)
But this does apparently not have the desired effect, because it only hooks to _G.SetCVar
whereas my code uses the local SetCVar.

Do I need hooksecurefunc() at all to hook to my local SetCVar?
Or what would the first argument of hooksecurefunc() have to be for a local function?

Thanks!

Kanegasi 12-19-19 03:14 PM

If I recall correctly, hook the function before you declare a local version. The hook propagates.

LudiusMaximus 12-19-19 03:19 PM

Quote:

Originally Posted by Kanegasi (Post 334809)
If I recall correctly, hook the function before you declare a local version. The hook propagates.

Ah, so I should not do the hook in the OnInitialize() function of my addon?

PS: It worked by the way! Thanks!

SDPhantom 12-19-19 08:01 PM

Functions are immutable. What hooksecurefunc() does is wrap the original function and your hook to run together from a new function, then overwrites the original with it. You can observe this by looking at the function pointer before and after hooking. They'll be different.

You can delay the hook and still have the local variable defined. You just need to update the pointer stored in the local after hooking.


All times are GMT -6. The time now is 01:03 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI