Wikia

WoWWiki

Changes: API hooksecurefunc

Edit

Back to page

m (Notes: No reason to prefix code with ">". Also, added note about :HookScript() being better than this function when you want to hook a frame's script handler.)
m (Fixed minor typo; exemple to example.)
 
Line 43: Line 43:
 
Also note that using hooksecurefunc() may not always produce the expected result. Keep in mind that it actually replaces the original function with a new one (the function reference changes).
 
Also note that using hooksecurefunc() may not always produce the expected result. Keep in mind that it actually replaces the original function with a new one (the function reference changes).
   
For exemple, if you try to hook the global function "MacroFrame_OnHide" (after the macro frame has been displayed), your function will not be called when the macro frame is closed.
+
For example, if you try to hook the global function "MacroFrame_OnHide" (after the macro frame has been displayed), your function will not be called when the macro frame is closed.
   
 
It's simply because in Blizzard_MacroUI.xml the OnHide function call is registered like this:
 
It's simply because in Blizzard_MacroUI.xml the OnHide function call is registered like this:

Latest revision as of 07:03, September 3, 2010

WoW API < hooksecurefunc

Creates a secure 'post hook' for the named function. Your hook will be called with the same arguments, but will not be able to affect the outcome of the original call.

hooksecurefunc([table,] "functionName", hookfunc)

ParametersEdit

ArgumentsEdit

table 
Optional Table - if you're hooking a function on an object, provide a reference to the object.
functionName 
String - the name of the function being hooked.
hookfunc 
Function - your hook function.

ExampleEdit

hooksecurefunc("CastSpellByName", print); -- Hooks the global CastSpellByName
hooksecurefunc(GameTooltip, "SetUnitBuff", print); -- Hooks GameTooltip.SetUnitBuff

ResultEdit

Hooks CastSpellByName and GameTooltip.SetUnitBuff without compromising their secure status. When those functions are called, prints their argument list into the default chat frame.

NotesEdit

This is the only safe way to hook functions that execute protected functionality.

The hookfunc is invoked after the original function, and receives the same parameters; return values from hookfunc are discarded.

If another addon, or the same addon, replaces functionName after you use this function to hook it, hookfunc will no longer fire. ... except of course if your new function calls the old original!

hooksecurefunc("ToggleBackpack", function(...)
    print("ToggleBackpack called.")
end)
ToggleBackpack = function(...)
    -- Do something different
end
ToggleBackpack() -- "ToggleBackpack called." never prints.

After hooksecurefunc() is used on a function, setfenv() throws an error when attempted on that function

You cannot "unhook" a function that is hooked with this function except by a UI reload. Calling hooksecurefunc() multiple times only adds more hooks to be called.

 hooksecurefunc("ToggleBackpack", function() print(1) end)
 hooksecurefunc("ToggleBackpack", function() print(2) end)
 ToggleBackpack()
 > 1
 > 2

Also note that using hooksecurefunc() may not always produce the expected result. Keep in mind that it actually replaces the original function with a new one (the function reference changes).

For example, if you try to hook the global function "MacroFrame_OnHide" (after the macro frame has been displayed), your function will not be called when the macro frame is closed.

It's simply because in Blizzard_MacroUI.xml the OnHide function call is registered like this:

<OnHide function="MacroFrame_OnHide"/>

The function ID called when the frame is hidden is the former one, before you hooked "MacroFrame_OnHide". The workaround is to hook a function called by "MacroFrame_OnHide":

hooksecurefunc(MacroPopupFrame, "Hide", MyFunction)

However, when you want to hook a frame's script handler, it's better to use Frame:HookScript like this:

MacroPopupFrame:HookScript("OnHide", MyFunction) -- the client will call MacroFrame_OnHide(MacroPopupFrame) first followed by MyFunction(MacroPopupFrame)

Around Wikia's network

Random Wiki