Thread: AceDB Resetting
View Single Post
08-08-16, 04:58 PM   #20
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Try stripping it down even more. Does this work?

Lua Code:
  1. local XIVBar = LibStub("AceAddon-3.0"):NewAddon("XIVBar")
  2.  
  3. XIVBar.defaults = {
  4.   profile = {
  5.     general = {
  6.       barPosition = "BOTTOM",
  7.     }
  8.   }
  9. }
  10.  
  11. function XIVBar:OnInitialize()
  12.   self.db = LibStub("AceDB-3.0"):New("XIVBarDB", self.defaults)
  13.   print("XIVBar:OnInitialize", self.db.profile.general.barPosition)
  14. end

Also, if you're going to do this:

lua Code:
  1. P = self.db.profile

... you also need to register for the callbacks AceDB fires when the profile is switched or reset, and update your "P" upvalue when those things happen. Otherwise, you'll still be referring to the old profile even though the user has chosen a different one.

Finally, this makes my brain hurt:

lua Code:
  1. local AddOnName, Engine = ...
  2. local XIVBar = LibStub("AceAddon-3.0"):NewAddon(AddOnName, "AceConsole-3.0", "AceEvent-3.0")
  3.  
  4. local L = LibStub("AceLocale-3.0"):GetLocale(AddOnName, false)
  5.  
  6. local P = {}
  7. Engine[1] = XIVBar
  8. Engine[2] = L
  9. Engine[3] = P
  10. _G.XIVBar = Engine
  11.  
  12. _G[AddOnName] = Engine

This pattern is awful. If you want XIVBar, L, and P to be accessible in all files, just do this:

lua Code:
  1. local AddonName, XIVBar = ...
  2. LibStub("AceAddon-3.0"):NewAddon(XIVBar, AddonName, "AceConsole-3.0", "AceEvent-3.0")
  3. -- ^ now your addon's shared table is an AceAddon object
  4.  
  5. local L = LibStub("AceLocale-3.0"):GetLocale(AddOnName)
  6. XIVBar.L = L

Generally speaking you don't need to explicitly create a global for your AceAddon addons. If you want to look it up in another file, or if another addon wants to access it, they can do LibStub("AceAddon-3.0"):GetAddon("XIVBar").

"P" shouldn't even be given a default value if you're just going to use it as a pointer to the current profile, and it definitely shouldn't be stored on the shared addon object where (presumably) you're going to assign "local P = Engine[3]" or whatever in other files, because (as noted above) you need to update that pointer if the user picked another profile.

Really, you should just not bother with a file-level upvalue for "self.db.profile". If you're calling it so many times in a given function that it's tedious to type/read, or in a function where speed is of the essence (eg. CLEU or OnUpdate), just use an upvalue local to the function:

lua Code:
  1. function XIVBar:DoLotsOfStuffWithSettings()
  2.     local profile = self.db.profile
  3.     -- use profile in here
  4. end

That saves you the trouble of registering for profile callbacks and updating higher-level upvalues.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote