WoW:Hooking functions: Difference between revisions

m (→‎Hooking functions with ":" in there name: Added a comment to clear up example)
Line 119: Line 119:
You can hook a function like "AddonName:Function()" by replaceing the ":" with a ".".
You can hook a function like "AddonName:Function()" by replaceing the ":" with a ".".
For example if the below code is from someone's addon:
For example if the below code is from someone's addon:
 
function AnAddon.Module:FuncName(per1,per2)
<div style="max-height: 200px; overflow: auto; border: 2px solid black; padding: 0.5em; margin-left: 0.5em">
  -- Things happen here...
<pre style="margin: 0; border: 0"><nowiki>
end;
function AnAddon.Module:FuncName(per1,per2)
-- Things happen here...
end;
</nowiki></pre></div>


You can use the following to pre-hook it:
You can use the following to pre-hook it:
 
local original_AnAddon_Module_FuncName = AnAddon.Module.FuncName; -- Store the original function encase you want to use it.
<div style="max-height: 200px; overflow: auto; border: 2px solid black; padding: 0.5em; margin-left: 0.5em">
function AnAddon.Module:FuncName(per1,per2) -- This is the new function to overwrite the original one
<pre style="margin: 0; border: 0"><nowiki>
  if (per1 > 10) then
local original_AnAddon_Module_FuncName = AnAddon.Module.FuncName; -- Store the original function encase you want to use it.
    ChatFrame1:AddMessage(tostring(per1));
 
  else
function AnAddon.Module:FuncName(per1,per2) -- This is the new function to overwrite the original one
    original_AnAddon_Module_FuncName(self,...); -- Calls original function like nothing happened
if (per1 > 10) then
  end;
ChatFrame1:AddMessage(tostring(per1));
end;
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.
Assuming per1 is a number greater than 10 then per2 will be printed and if per1 isn't then the original function will run.