WoW:API Frame HookScript: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (New page: {{widgetmethod}} Post hooks a script handler (secure). frame:HookScript("handler", function) == Parameters == === Arguments === <!-- List each argument, together with its type --> :("h...)
 
m (Move page script moved page API Frame HookScript to API Frame HookScript without leaving a redirect)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{widgetmethod}}
{{widgetmethod}}


Post hooks a script handler (secure).
Sets a function to be called automatically after the original function is called, or "post hook". Works with secure frames. (secure)
 
  frame:HookScript("handler", func)
  frame:HookScript("handler", function)


== Parameters ==
== Parameters ==
=== Arguments ===
=== Arguments ===
<!-- List each argument, together with its type -->
:;handler
:("handler", func)
:: string - the name of the handler type, like 'OnShow', 'OnClick'. See [[Widget handlers]].
:;func
:: function - function to be called. Cannot be 'nil'.


:;handler : String - The handler to attach func to (OnShow, OnEvent, [[Widget_handlers|et al]])
== Summary ==
:;func : Function - The function to call. Unlike [[API_Frame_SetScript|:SetScript()]], <tt>nil</tt> throws an error. <tt>func</tt> is called with arguments <tt>(frame, arg1, arg2, ...)</tt> (<tt>event</tt> being <tt>arg1</tt> for OnEvent handlers)
'HookScript' can be used on secure frames, where 'SetScript' would cause "taint" issues and bad UI behavior. Calls each hooked function after calling its original handler function. Any values returned by the hooking function will be discarded, and not be passed to the calling function (which is usually secure code). Any handler added this way cannot be removed, and will stay until the UI is reloaded.


== Example ==
== Examples ==
local MyPartyFrame_OnClick = function( self, button )
    print(self:GetName() .. " clicked with " .. button)
end
  for i = 1, 4 do
  for i = 1, 4 do
     local frame = _G["PartyMemberFrame"..i]
     local frame = _G["PartyMemberFrame"..i]
     frame:HookScript("OnClick", function(self, button)
     frame:HookScript("OnClick", MyPartyFrame_OnClick)
        print(self:GetName() .. " clicked with " .. button)
    end)
  end
  end
 
Print notification in chat log when the player clicks a party member's portrait. 'HookScript' works while preserving the security of the unit frame after calling its original handler, where 'SetScript' in this case would not.
====Result====
Alert the player when he clicks a party member's portrait while preserving the security of the unit frame after calling its original handler.


== Notes ==
== Notes ==
* Just like [[API_hooksecurefunc|hooksecurefunc]], this can be used on secure code.
* Like any event handler, 'func' is called with arguments '(frame, arg1, arg2, ...)', 'event' being 'arg1' for OnEvent handlers.
* If the frame doesn't have a ''func'' for ''handler'', one will be created as if you called [[API_Frame_SetScript|:SetScript("handler", func)]].
* Just like {{api|hooksecurefunc}}("handler", func), this can be used on secure code.
* Every script handler has at least one argument (the frame itself, usually ''self''). See [[widget handlers]] for details on each handler.
* If the frame doesn't have a 'func' for 'handler', one will be created as if you called {{api|Frame:SetScript|t=w}}().
* Every script handler has at least one argument (the frame itself, usually ''self'').
* See [[widget handlers]] for details on each handler.


__NOTOC__
== See also ==
* {{api|hooksecurefunc}}
* {{api|Frame:SetScript|t=w}}
* {{api|Frame:GetScript|t=w}}
* [[Widget handlers]]

Latest revision as of 04:45, 15 August 2023

Widget API ← Frame < HookScript

Sets a function to be called automatically after the original function is called, or "post hook". Works with secure frames. (secure)

frame:HookScript("handler", func)

Parameters[edit]

Arguments[edit]

handler
string - the name of the handler type, like 'OnShow', 'OnClick'. See Widget handlers.
func
function - function to be called. Cannot be 'nil'.

Summary[edit]

'HookScript' can be used on secure frames, where 'SetScript' would cause "taint" issues and bad UI behavior. Calls each hooked function after calling its original handler function. Any values returned by the hooking function will be discarded, and not be passed to the calling function (which is usually secure code). Any handler added this way cannot be removed, and will stay until the UI is reloaded.

Examples[edit]

local MyPartyFrame_OnClick = function( self, button )
    print(self:GetName() .. " clicked with " .. button)
end
for i = 1, 4 do
    local frame = _G["PartyMemberFrame"..i]
    frame:HookScript("OnClick", MyPartyFrame_OnClick)
end

Print notification in chat log when the player clicks a party member's portrait. 'HookScript' works while preserving the security of the unit frame after calling its original handler, where 'SetScript' in this case would not.

Notes[edit]

  • Like any event handler, 'func' is called with arguments '(frame, arg1, arg2, ...)', 'event' being 'arg1' for OnEvent handlers.
  • Just like hooksecurefunc("handler", func), this can be used on secure code.
  • If the frame doesn't have a 'func' for 'handler', one will be created as if you called Frame:SetScript().
  • Every script handler has at least one argument (the frame itself, usually self).
  • See widget handlers for details on each handler.

See also[edit]