m
Move page script moved page Hooking functions to WoW:Hooking functions without leaving a redirect
mNo edit summary |
m (Move page script moved page Hooking functions to WoW:Hooking functions without leaving a redirect) |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
{{ | {{wow/uihowto}} | ||
'''Hooking functions''' allows you to replace a function value in an accessible environment by a function of your own, calling the original function before or after execution of new code. Hooking enables addons to modify and/or extend the behavior of the default UI and API. | '''Hooking functions''' allows you to replace a function value in an accessible environment by a function of your own, calling the original function before or after execution of new code. Hooking enables addons to modify and/or extend the behavior of the default UI and API. | ||
Depending on how the behavior of the original function is | Depending on how the behavior of the original function is modified, one or more of three hook types may be used: | ||
;Pre-hooks : New code executes before calling the original function, potentially choosing to modify the parameters passed to it, or not call it at all | ;Pre-hooks : New code executes before calling the original function, potentially choosing to modify the parameters passed to it, or not call it at all | ||
;Post-hooks : New code executes after the original function, potentially using the original function's return values. | ;Post-hooks : New code executes after the original function, potentially using the original function's return values. | ||
| Line 20: | Line 20: | ||
== Hooking a function == | == Hooking a function == | ||
To illustrate the concept, let's create a pre-hook. Pre-hooks are commonly used to override user interface functions that perform an (unprotected) action, potentially adding behavior to existing UI. In this example, hook {{api|ChatFrame_OnHyperlinkShow}} (a function called when the user clicks on a chat link) to display a faux tooltip for the | To illustrate the concept, let's create a pre-hook. Pre-hooks are commonly used to override user interface functions that perform an (unprotected) action, potentially adding behavior to existing UI. In this example, hook {{api|ChatFrame_OnHyperlinkShow}} (a function called when the user clicks on a chat link) to display a faux tooltip for the item Tinfoil Hat, an item currently not in the game. | ||
<div style="max-height: 200px; overflow: auto; border: 2px solid black; padding: 0.5em; margin-left: 0.5em"> | <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 origChatFrame_OnHyperlinkShow = ChatFrame_OnHyperlinkShow; -- (1) | <pre style="margin: 0; border: 0"><nowiki>local origChatFrame_OnHyperlinkShow = ChatFrame_OnHyperlinkShow; -- (1) | ||