InCombatLockdown() does not return true immediately when PLAYER_REGEN_DISABLED fires; this is intentional, so that addons have time to do stuff with secure frames when the player enters or exits combat, so your code may not always work as desired. UnitAffectingCombat("player") would be a more reliable check.
There's also no reason to do a _G lookup when you already have the frame's global name. Just run NameOfFrameToTrack:Show() instead of _G["NameOfFrameToTrack"]:Show(), and save the extra lookup.
You may also want to register PLAYER_ENTERING_WORLD.
I'd change/simplify the whole thing to:
Code:
local addon = CreateFrame("Frame")
--register events
addon:RegisterEvent("PLAYER_ENTERING_WORLD")
addon:RegisterEvent("PLAYER_REGEN_DISABLED")
addon:RegisterEvent("PLAYER_REGEN_ENABLED")
-- call func based on event
addon:SetScript("OnEvent", function(self, event, ...)
if UnitAffectingCombat("player") then
-- in combat combat
NameOfFrameToTrack:Hide()
else
-- not in combat combat
NameOfFrameToTrack:Show()
end
end)
If you're using this in kgPanels, change "addon" to "self", put the RegisterEvent lines in the OnLoad section for the panel, and everything inside the OnEvent script function in the OnEvent section for the panel.