Thread Tools Display Modes
11-01-23, 10:42 AM   #1
Nieub Barmy
A Murloc Raider
 
Nieub Barmy's Avatar
Join Date: Nov 2022
Posts: 4
Script to refresh an addon

Hi,

I created an addon to make changes to my UI and Sound depending on the state of my character (in combat/ out of combat/ resting), unfortunately the addon only works at the point in time where my character changes state, like when my character enters combat or walks into an inn for example.

However, if I log into game when my character is in the Inn then the UI and sound are not as required and I have to leave the Inn and enter it again for the changes to be made.

Is there a script that can be added to the addon that checks the status of the character regularly and updates as required?

https://pastebin.com/rNStNC0r


Thanks in advance
  Reply With Quote
11-01-23, 11:10 AM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Delay registering your PRD and PUD events until after either ADDON_LOADED or PLAYER_ENTERING_WORLD. You are likely registering PRD and PUD too early in the loading process. I'd use PEW to be on the safe side; it is late enough in the loading process that most other events will function when hooked.

Side note: you don't need to create 3 frames. Create one, register PEW on it, register the other events during PEW, and check if event == blah, elseif event == blah2, elseif event == blah3, end.

Last edited by myrroddin : 11-01-23 at 11:14 AM. Reason: clarity
  Reply With Quote
11-01-23, 12:21 PM   #3
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,929
Also consider that some in game changes may not apply without a reload, even if activated at the right time.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
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,326
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

WoWInterface » Developer Discussions » Lua/XML Help » Script to refresh an addon


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off