m
→Hooking functions with ":" in there name: Added a comment to clear up example
m (Robot: Automated text replacement (-[[Image: +[[File:)) |
m (→Hooking functions with ":" in there name: Added a comment to clear up example) |
||
| Line 65: | Line 65: | ||
The example used the vararg expression to make sure that, should the API change, the hook will still pass exactly the arguments it was given (no more, no less) to the original function. An additional sanity check on the type of the link argument in 3 prevents a potential error: if the function was not passed a string as expected (for example, by another addon), we do not want to block the default behavior. | The example used the vararg expression to make sure that, should the API change, the hook will still pass exactly the arguments it was given (no more, no less) to the original function. An additional sanity check on the type of the link argument in 3 prevents a potential error: if the function was not passed a string as expected (for example, by another addon), we do not want to block the default behavior. | ||
* ''You can hook a function like "AddonName:Function()" by replaceing the ":" with a "."'' | |||
== Post-hooking == | == Post-hooking == | ||
| Line 115: | Line 115: | ||
end | end | ||
Libraries may contain functions that allow you to "unhook" your method -- those operate primarily by moving the ignore switch further up the execution tree: if you want to remove your hook, it won't get called, but the library's hook function stays in place. | Libraries may contain functions that allow you to "unhook" your method -- those operate primarily by moving the ignore switch further up the execution tree: if you want to remove your hook, it won't get called, but the library's hook function stays in place. | ||
== Hooking functions with ":" in there name == | |||
You can hook a function like "AddonName:Function()" by replaceing the ":" with a ".". | |||
For example if the below code is from someone's addon: | |||
<div style="max-height: 200px; overflow: auto; border: 2px solid black; padding: 0.5em; margin-left: 0.5em"> | |||
<pre style="margin: 0; border: 0"><nowiki> | |||
function AnAddon.Module:FuncName(per1,per2) | |||
-- Things happen here... | |||
end; | |||
</nowiki></pre></div> | |||
You can use the following to pre-hook it: | |||
<div style="max-height: 200px; overflow: auto; border: 2px solid black; padding: 0.5em; margin-left: 0.5em"> | |||
<pre style="margin: 0; border: 0"><nowiki> | |||
local original_AnAddon_Module_FuncName = AnAddon.Module.FuncName; -- Store the original function encase you want to use it. | |||
function AnAddon.Module:FuncName(per1,per2) -- This is the new function to overwrite the original one | |||
if (per1 > 10) then | |||
ChatFrame1:AddMessage(tostring(per1)); | |||
else | |||
original_AnAddon_Module_FuncName(self,...); -- Calls original function like nothing happened | |||
end; | |||
end; | |||
</nowiki></pre></div> | |||
Assuming per1 is a number greater than 10 then per2 will be printed and if per1 isn't then the original function will run. | |||
== Notes == | == Notes == | ||
* Warning! Blizzard has moved some pieces of the UI to be loaded on demand. The functions in those pieces cannot be hooked until they are loaded. | * Warning! Blizzard has moved some pieces of the UI to be loaded on demand. The functions in those pieces cannot be hooked until they are loaded. | ||
* Some libraries, like [[Sea]], [[Ace]], or [[Ace2]] offer functions to assist in hooking. | * Some libraries, like [[Sea]], [[Ace]], or [[Ace2]] offer functions to assist in hooking. | ||