I think the event registering is fine aside from the use of multiple frames when one would do. The issue is these events fire when the state changes, if you're already in certain states when your addon loads, the events won't fire.
As for event suggestions mentioned previously,
ADDON_LOADED fires when all of an addon's files have been loaded and the main chunk of all of its Lua files has been executed. This also signifies that SavedVars for the addon has been loaded as well.
PLAYER_ENTERING_WORLD fires any time when a loading screen finishes. This has historically been used to delay a loading process to when player data is available, but there is another event more suited for that.
PLAYER_LOGIN fires specifically when general player data is accessible.
Lastly, your code produces 4 states in which one creates a conflict with itself. This is when you're in combat in a rested area. Here's some sample code along with some optimization.
Lua Code:
local Profile_Combat="In Combat";
local Profile_Resting="Rested";
local Profile_Default="Default";
local CombatCamera_Zoom=11;
local CameraSlot_Resting=1;
local CameraSlot_Default=2;
local LastCombat,LastResting;-- Let these initiate to nil so we force an update on first fire
local function UpdateState(combatstate)
if InCombatLockdown() then return; end-- Can't call protected functions while in combat (PLAYER_REGEN_DISABLED fires before lockdown happens)
local reststate=IsResting();
-- We're not in combat if we get here, we have to rely on being passed true to force the state
local combatchanged,restingchanged=(combatstate~=LastCombat),(reststate~=LastResting);
if combatchanged or restingchanged then
local profile=(
(combatstate and Profile_Combat)
or (reststate and Profile_Resting)
or Profile_Default
);
Dominos.db:SetProfile(profile);
ShadowUF.db:SetProfile(profile);
if restingchanged then
SetView(resting and CameraSlot_Resting or CameraSlot_Default);
SetCVar("Sound_EnableMusic",reststate and 1 or 0);
end
if combatchanged then
if combatstate then CameraZoomIn(CombatCamera_Zoom); else CameraZoomOut(CombatCamera_Zoom); end
end
end
LastCombat,LastResting=combatstate,reststate;
end
-- Since we don't need a frame for anything else, we can use the built-in EventRegistry
local function DefaultStateHandler() return UpdateState(false); end
local function CombatStateHandler() return UpdateState(true); end
EventRegistry:RegisterFrameEventAndCallback("PLAYER_LOGIN",DefaultStateHandler);
EventRegistry:RegisterFrameEventAndCallback("PLAYER_REGEN_DISABLED",CombatStateHandler);
EventRegistry:RegisterFrameEventAndCallback("PLAYER_REGEN_ENABLED",DefaultStateHandler);
EventRegistry:RegisterFrameEventAndCallback("PLAYER_UPDATE_RESTING",DefaultStateHandler);
I don't know what you intend to do with the camera now the code has been fixed to 3 states, prioritizing combat mode over rested and resolving the conflict between the two. I believe camera zoom conflicts with
SetView(), so some additional tweaking may be required. It's also a good idea to put values you may want to tweak later as constants at the top of the file. Especially with larger projects, you'll thank yourself if you want to change these months later and forgot all the places they're used in.