View Single Post
11-09-23, 05:50 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,330
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:
  1. local Profile_Combat="In Combat";
  2. local Profile_Resting="Rested";
  3. local Profile_Default="Default";
  4.  
  5. local CombatCamera_Zoom=11;
  6.  
  7. local CameraSlot_Resting=1;
  8. local CameraSlot_Default=2;
  9.  
  10. local LastCombat,LastResting;-- Let these initiate to nil so we force an update on first fire
  11. local function UpdateState(combatstate)
  12.     if InCombatLockdown() then return; end--    Can't call protected functions while in combat (PLAYER_REGEN_DISABLED fires before lockdown happens)
  13.     local reststate=IsResting();
  14.  
  15. --  We're not in combat if we get here, we have to rely on being passed true to force the state
  16.     local combatchanged,restingchanged=(combatstate~=LastCombat),(reststate~=LastResting);
  17.     if combatchanged or restingchanged then
  18.         local profile=(
  19.             (combatstate and Profile_Combat)
  20.             or (reststate and Profile_Resting)
  21.             or Profile_Default
  22.         );
  23.  
  24.         Dominos.db:SetProfile(profile);
  25.         ShadowUF.db:SetProfile(profile);
  26.  
  27.         if restingchanged then
  28.             SetView(resting and CameraSlot_Resting or CameraSlot_Default);
  29.             SetCVar("Sound_EnableMusic",reststate and 1 or 0);
  30.         end
  31.  
  32.         if combatchanged then
  33.             if combatstate then CameraZoomIn(CombatCamera_Zoom); else CameraZoomOut(CombatCamera_Zoom); end
  34.         end
  35.     end
  36.  
  37.     LastCombat,LastResting=combatstate,reststate;
  38. end
  39.  
  40. --  Since we don't need a frame for anything else, we can use the built-in EventRegistry
  41. local function DefaultStateHandler() return UpdateState(false); end
  42. local function CombatStateHandler() return UpdateState(true); end
  43. EventRegistry:RegisterFrameEventAndCallback("PLAYER_LOGIN",DefaultStateHandler);
  44. EventRegistry:RegisterFrameEventAndCallback("PLAYER_REGEN_DISABLED",CombatStateHandler);
  45. EventRegistry:RegisterFrameEventAndCallback("PLAYER_REGEN_ENABLED",DefaultStateHandler);
  46. 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.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote