WoW:API hooksecurefunc: Difference between revisions
(→Notes) |
(→Example: Use the 3.0 print.) |
||
Line 11: | Line 11: | ||
==Example== | ==Example== | ||
hooksecurefunc("CastSpellByName", print); -- Hooks the global CastSpellByName | |||
hooksecurefunc(GameTooltip, "SetUnitBuff", print); -- Hooks GameTooltip.SetUnitBuff | |||
hooksecurefunc("CastSpellByName", | |||
hooksecurefunc(GameTooltip, "SetUnitBuff", | |||
===Result=== | ===Result=== | ||
Hooks CastSpellByName and GameTooltip.SetUnitBuff without compromising their secure status. When those functions are called, prints their argument list into the default chat frame. | Hooks CastSpellByName and GameTooltip.SetUnitBuff without compromising their secure status. When those functions are called, prints their argument list into the default chat frame. |
Revision as of 21:11, 21 February 2009
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)
Parameters
Arguments
- 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.
Example
hooksecurefunc("CastSpellByName", print); -- Hooks the global CastSpellByName hooksecurefunc(GameTooltip, "SetUnitBuff", print); -- Hooks GameTooltip.SetUnitBuff
Result
Hooks CastSpellByName and GameTooltip.SetUnitBuff without compromising their secure status. When those functions are called, prints their argument list into the default chat frame.
Notes
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