Thread Tools Display Modes
07-26-16, 05:06 PM   #1
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Hooking a function used for OnUpdate.

Using the code below, the function called for OnUpdate won't get hooked using hooksecurefunc but will if you change it to HookScript.

hooksecurefunc will work for functions used for other scripts.

I don't believe this was the way it worked previously.

Lua Code:
  1. function SomeDopeyfunc(self)
  2. print("A")
  3. end
  4.  
  5. local f = CreateFrame("Frame", "SomeDopeyFrameName")
  6. f:SetSize(20, 20)
  7. f:SetScript("OnUpdate", SomeDopeyfunc)
  8.  
  9. local function SomeOtherfunc(self)
  10. print(self)
  11. end
  12.  
  13. --SomeDopeyFrameName:HookScript("OnUpdate", SomeOtherfunc)
  14. hooksecurefunc("SomeDopeyfunc", SomeOtherfunc)

Edit: Is this intended? Changing SomeDopeyfunc to a local function I get:
[code]
1x A\A.lua:16: hooksecurefunc(): SomeDopeyfunc is not a function
[C]: in function `hooksecurefunc'
A\A.lua:16: in main chunk

[code]
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 07-26-16 at 05:25 PM.
 
07-26-16, 06:22 PM   #2
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
I think you want to do:

Lua Code:
  1. hooksecurefunc(SomeDopeyFrameName, "OnUpdate", function(self, elapsed)
  2.     print(self:GetName(), elapsed)
  3. end)

hooksecurefunc is designed to catch secure but mainly global functions, so thats why it might not be able to handle local ones.
 
07-26-16, 06:26 PM   #3
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Originally Posted by Resike View Post
I think you want to do:

Lua Code:
  1. hooksecurefunc(SomeDopeyFrameName, "OnUpdate", function(self, elapsed)
  2.     print(self:GetName(), elapsed)
  3. end)

hooksecurefunc is designed to catch secure but mainly global functions, so thats why it might not be able to handle local funcitons.
I can use the original form to hook a function being used for an OnEvent script without problem. I'm pretty sure it was usable for OnUpdate pre 7.x.

I wasn't sure about the local thing.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
 
07-26-16, 07:44 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
The syntax is hooksecurefunc([table,]index,function). If table is omitted, _G is assumed. The entire design of the function is to hook another function stored in index of table. This means it has to be accessible from the global namespace or a provided table. hooksecurefunc() doesn't operate on locals and trying to use it on a frame won't necessarily work unless someone is dumb enough to have a dynamic function like this: frame:SetScript("OnUpdate",function(self) self:OnUpdate(); end).

In addition, if any function is directly assigned as a handler, changing it in any way outside of Frame:HookScript() won't update the function pointer used. Meaning the original function as defined would be updated, but the reference given to the frame to use for a handler will still point to the old function instead of your hook.

For example:
Code:
function A() print("A"); end
function B() print("B"); end

local frame=CreateFrame("Frame");
frame:SetScript("OnUpdate",A);

hooksecurefunc("A",B);
From your logic, you're thinking it would print A and B on alternating lines, but you'll only see A bring printed. What hooksecurefunc() is doing in this case is dynamicly creating a 3rd function that runs function A and B, passing the returns along from the first. The pointer to this new function then overwrites the old pointer in the global namespace. The pointer given to the frame for its handler remains unchanged.

The end result is, if you need to hook a script handler, use Frame:HookScript(), that's what it's there for.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 07-26-16 at 07:47 PM.
 
07-26-16, 08:34 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
I think I need to reevaluate my test.

Apparently this addon was working pre-7.x hooking the function used by OnUpdate and no longer does after clearing the error by changing line 14 to

Code:
local textDisplay	= self.Text--getglobal(self:GetName().."Text")
Something to ponder while I'm away, Thank you.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
 
07-26-16, 10:33 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
My guess is they updated the XML.

From:
Code:
<OnUpdate>CastingBarFrame_OnUpdate(self,elapsed)</OnUpdate>
To:
Code:
<OnUpdate function="CastingBarFrame_OnUpdate"/>
The first creates an intermediate function that calls CastingBarFrame_OnUpdate(), the second sets CastingBarFrame_OnUpdate() as the handler. This is why the hook worked pre-7.0.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
 
07-27-16, 04:26 AM   #7
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Didn't realise hooksecurefunc simply replaced the hooked function, but that was easily confirmed.
Code:
local a = print
hooksecurefunc("print", function() end)
print(print == a) -- false
__________________
Grab your sword and fight the Horde!
 
07-27-16, 05:50 AM   #8
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Originally Posted by Lombra View Post
Didn't realise hooksecurefunc simply replaced the hooked function, but that was easily confirmed.

Same, I only found out about that when I tried to see with hooksecurefunc whether RemoveExtraSpaces/RunScript was changed, or pointing to something else
Thought I was going crazy when the reference kept changing after I hooked it... every day you learn something new ><

http://wow.gamepedia.com/API_hooksecurefunc
Keep in mind that it actually replaces the original function with a new one (the function reference changes)
 
 

WoWInterface » Site Forums » Archived Beta Forums » Legion Beta archived threads » Hooking a function used for OnUpdate.

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off