Thread Tools Display Modes
12-19-19, 02:52 PM   #1
LudiusMaximus
A Rage Talon Dragon Guard
 
LudiusMaximus's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2018
Posts: 320
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!
  Reply With Quote
12-19-19, 03:14 PM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
If I recall correctly, hook the function before you declare a local version. The hook propagates.
  Reply With Quote
12-19-19, 03:19 PM   #3
LudiusMaximus
A Rage Talon Dragon Guard
 
LudiusMaximus's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2018
Posts: 320
Originally Posted by Kanegasi View Post
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!

Last edited by LudiusMaximus : 12-19-19 at 03:21 PM.
  Reply With Quote
12-19-19, 08:01 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
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.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 12-19-19 at 08:04 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » hooksecurefunc() with local versions of functions?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off