WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Handler callback function stops working, why? (https://www.wowinterface.com/forums/showthread.php?t=56219)

Numba1stunna 05-13-18 06:58 PM

Handler callback function stops working, why?
 
Can someone explain to me why this doesn't work? Do hidden frames not execute "OnUpdate" script handler? I am confused. This is an extremely simple UI addon which shows an icon on target/focus if they're in combat or not in combat. I localized each function for each handler, but it seems that once the frame is hidden, it can't execute the callback anymore. Why?

Lua Code:
  1. --combat script
  2. local UnitAffectingCombat = UnitAffectingCombat;
  3.  
  4. local TargetCombatFrame=CreateFrame("Frame")
  5. TargetCombatFrame:SetParent(TargetFrame)
  6. TargetCombatFrame:SetPoint("LEFT",TargetFrame,"RIGHT",0,0)
  7. TargetCombatFrame:SetSize(35,35)
  8. TargetCombatFrame.t=TargetCombatFrame:CreateTexture(nil,BORDER)
  9. TargetCombatFrame.t:SetAllPoints()
  10. TargetCombatFrame.t:SetTexture("Interface\\Icons\\Ability_Sap")
  11. TargetCombatFrame:Show()
  12.  
  13. local function FrameOnUpdate(self,elapsed)
  14.     print("test1")
  15.     if not UnitAffectingCombat("target") then
  16.         self:Show()
  17.     else
  18.         self:Hide()
  19.     end
  20. end
  21. TargetCombatFrame:SetScript("OnUpdate",FrameOnUpdate)
  22.  
  23. local FocusCombatFrame=CreateFrame("Frame")
  24. FocusCombatFrame:SetParent(FocusFrame)
  25. FocusCombatFrame:SetPoint("RIGHT",FocusFrame,"LEFT",0,0)
  26. FocusCombatFrame:SetSize(35,35)
  27. FocusCombatFrame.t=FocusCombatFrame:CreateTexture(nil,BORDER)
  28. FocusCombatFrame.t:SetAllPoints()
  29. FocusCombatFrame.t:SetTexture("Interface\\Icons\\Ability_Sap")
  30. FocusCombatFrame:Show()
  31.  
  32. local function FrameOnUpdate(self,elapsed)
  33.     print("test2")
  34.     if not UnitAffectingCombat("focus") then
  35.         self:Show()
  36.     else
  37.         self:Hide()
  38.     end
  39. end
  40. FocusCombatFrame:SetScript("OnUpdate",FrameOnUpdate)

Fizzlemizz 05-13-18 08:42 PM

Hiding a frame stops the OnUpdate process until the frame is shown again. You could make it fully transparent.

SDPhantom 05-19-18 12:41 AM

Here's what I came up with as well as improving the code.

Lua Code:
  1. local UnitAffectingCombat=UnitAffectingCombat;
  2. local SecureButton_GetUnit=SecureButton_GetUnit;
  3.  
  4. local Textures={};--    Texture object lookup table
  5. local function OnUpdate(self) Textures[self]:SetShown(UnitAffectingCombat(SecureButton_GetUnit(self))); end
  6.  
  7. local function SetupCombatTexture(frame,...)--  Passes vararg to :SetPoint()
  8.     if not Textures[frame] then
  9.         local tex=frame:CreateTexture(nil,"BORDER");
  10.         tex:SetPoint(...);--    Set point from vararg
  11.         tex:SetSize(35,35);
  12.         tex:SetTexture("Interface\\Icons\\Ability_Sap");
  13.  
  14.         Textures[frame]=tex;--  Store texture in our lookup table
  15.         frame:HookScript("OnUpdate",OnUpdate);--    Register our hook
  16.     end
  17. end
  18.  
  19. SetupCombatTexture(TargetFrame,"LEFT",TargetFrame,"RIGHT");
  20. SetupCombatTexture(FocusFrame,"RIGHT",FocusFrame,"LEFT");

This simply creates textures on the existing frame you were attaching them to instead of making dynamic frames for that purpose. Since you can hook frame scripts using :SetHook(), there really isn't a need for extra frames for script handling. This also uses the fact that TargetFrame and FocusFrame inherit SecureUnitButtonTemplate, which allow using SecureButton_GetUnit() to get their set unit instead of hard-coding them. This will allow you to reuse the code for other frames that inherit the same template.

Numba1stunna 05-19-18 01:43 AM

Nice use of SecureButton_GetUnit, I didn't even know that existed. There's probably more API I don't know about.


All times are GMT -6. The time now is 06:53 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI