WoW:API hooksecurefunc: Difference between revisions

mNo edit summary
Line 26: Line 26:
The ''hookfunc'' is invoked after the original function, and receives the same parameters; return values from hookfunc are discarded.  
The ''hookfunc'' is invoked after the original function, and receives the same parameters; return values from hookfunc are discarded.  


You can only hook a global function using this function.
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!
 
If another addon, or the same addon, replaces ''functionName'' after you use this function to hook it, ''hookfunc'' will no longer fire.
  hooksecurefunc("ToggleBackpack", function(...)
  hooksecurefunc("ToggleBackpack", function(...)
     print("ToggleBackpack called.")
     print("ToggleBackpack called.")
Line 39: Line 37:
After hooksecurefunc() is used on a function, setfenv() throws an error when attempted on that function
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.
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.
-- this doesn't work as expected
 
  hooksecurefunc("ToggleBackpack", function() print(1) end)
-- Make SetItemRef output the item to the chat frame
  hooksecurefunc("ToggleBackpack", function() print(2) end)
hooksecurefunc('SetItemRef', function(item)
  ToggleBackpack()
    ChatFrame1:AddMessage(item)
  > 1
)
  > 2
-- unhook, maybe. I don't think do. It produces an error and SetItemRef() still prints to the chat frame.
-- Error: "hooksecurefunc('SetItemRef', nil)...":1: Usage: hooksecurefunc([table,] "function", hookfunc)
hooksecurefunc('SetItemRef', nil)
-- change the hook? Nope. No error, but SetItemRef() still prints to the chat frame and then the item is saved to MySavedVariable.
hooksecurefunc('SetItemRef', function(item)
    MySavedVariable = item
)
Anonymous user