Here's what I came up with as well as improving the code.
Lua Code:
local UnitAffectingCombat=UnitAffectingCombat;
local SecureButton_GetUnit=SecureButton_GetUnit;
local Textures={};-- Texture object lookup table
local function OnUpdate(self) Textures[self]:SetShown(UnitAffectingCombat(SecureButton_GetUnit(self))); end
local function SetupCombatTexture(frame,...)-- Passes vararg to :SetPoint()
if not Textures[frame] then
local tex=frame:CreateTexture(nil,"BORDER");
tex:SetPoint(...);-- Set point from vararg
tex:SetSize(35,35);
tex:SetTexture("Interface\\Icons\\Ability_Sap");
Textures[frame]=tex;-- Store texture in our lookup table
frame:HookScript("OnUpdate",OnUpdate);-- Register our hook
end
end
SetupCombatTexture(TargetFrame,"LEFT",TargetFrame,"RIGHT");
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.