WoW:API hooksecurefunc: Difference between revisions

m
Move page script moved page API hooksecurefunc to WoW:API hooksecurefunc without leaving a redirect
(→‎Example: Use the 3.0 print.)
m (Move page script moved page API hooksecurefunc to WoW:API hooksecurefunc without leaving a redirect)
 
(3 intermediate revisions by 3 users not shown)
Line 36: Line 36:
   hooksecurefunc("ToggleBackpack", function() print(1) end)
   hooksecurefunc("ToggleBackpack", function() print(1) end)
   hooksecurefunc("ToggleBackpack", function() print(2) end)
   hooksecurefunc("ToggleBackpack", function() print(2) end)
   ToggleBackpack()
   ToggleBackpack()
   > 1
   > 1
   > 2
   > 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 [[API_Frame_HookScript|Frame:HookScript]] like this:
MacroPopupFrame:HookScript("OnHide", MyFunction) -- the client will call MacroFrame_OnHide(MacroPopupFrame) first followed by MyFunction(MacroPopupFrame)
Anonymous user