It'll probably help to consolidate the logic into a single function that evaluates the entire list of conditions in which you want the UnitFrames to show. Additional "handlers" were added to link the events and mouse actions to the visibility logic.
Lua Code:
-- Configuration
local ShowAlpha=1;
local HideAlpha=0.05;
-- Visibility logic
local function UnitFrame_UpdateVisibility(self,force)
self:SetAlpha(
(force or (force==nil and (-- Allow sending true/false to override logic, leave as nil to allow
InCombatLockdown()-- Check combat
or UnitIsUnit("target","player")-- Check targeting self
or self:IsMouseOver()-- Check mouseover
))) and ShowAlpha or HideAlpha
);
end
-- Shared event handler
local function EventRegistry_OnEvent(self,event,...)
local force=(event=="PLAYER_REGEN_DISABLED" or nil);-- InCombatLockdown() returns false when PLAYER_REGEN_DISABLED fires (we cast false to nil to allow the visibility logic to run)
UnitFrame_UpdateVisibility(PlayerFrame,force);
UnitFrame_UpdateVisibility(TargetFrame,force);
end
EventRegistry:RegisterFrameEventAndCallback("PLAYER_REGEN_DISABLED",EventRegistry_OnEvent);
EventRegistry:RegisterFrameEventAndCallback("PLAYER_REGEN_ENABLED",EventRegistry_OnEvent);
EventRegistry:RegisterFrameEventAndCallback("PLAYER_TARGET_CHANGED",EventRegistry_OnEvent);
-- Shared mouse handler (This is meant to trim the arguments passed to the script so it doesn't mess with the visibility logic)
local function UnitFrame_OnMouseMotion(self)
UnitFrame_UpdateVisibility(self);-- "force" argument is omitted, so it naturally gets nil
end
PlayerFrame:HookScript("OnEnter",UnitFrame_OnMouseMotion);
PlayerFrame:HookScript("OnLeave",UnitFrame_OnMouseMotion);
TargetFrame:HookScript("OnEnter",UnitFrame_OnMouseMotion);
TargetFrame:HookScript("OnLeave",UnitFrame_OnMouseMotion);
PS:
EventRegistry can be a little confusing as its a shared system that can handle both internal callbacks and frame events. That being said, frame events require the event to be registered through
EventRegistry:RegisterFrameEvent() in order for your callback registered with
EventRegistry:RegisterCallback() to run. You can do both in a single call with
EventRegistry:RegisterFrameEventAndCallback().
My guess is since this wasn't an issue for you, some other code (possibly not yours) is already using those events and had registered them properly. You shouldn't rely on this as it might not always be the case and
EventRegistry runs a counter on how many functions are linked to any specific frame event. If those counters get out of sync by incomplete registrations, you may find yourself randomly no longer getting event triggers.