WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   AceDB lost default values on SetProfile (https://www.wowinterface.com/forums/showthread.php?t=55744)

maqjav 09-17-17 02:10 PM

AceDB lost default values on SetProfile
 
Hello.

I'm trying to use profiles in one of my addons and everything works fine until I change the profile, where I lose every default value.

Let's say I have the next code:

Code:

local PROFILE_DEFAULTS = {
  profile = {
    defaultValue = true,
    anotherValue = "default"
  }
}

function MyAddon:OnInitialize()
  self.db = LibStub("AceDB-3.0"):New("MyAddonDB", PROFILE_DEFAULTS)
  private.db = self.db
end


Now I change some settings where I change the value of "anotherValue" to "changed".
When I logout, I can see in my savedVariables.lua the next table:
Code:

["profileKeys"] = {
        ["myCharacter"] = "profile1",
},
["profiles"] = {
        ["profile1"] = {
                ["anotherValue"] = "changed"
        }
},

If I try to set this profile to a second character, I get the next values:
Code:

private.db:SetProfile("profile1")

print(private.db.profile.anotherValue) --prints changed
print(private.db.profile.defaultValue) --prints nil

Somehow after changing the profile my default values are lost.
If I reset the UI then everything works again, and I have both values.

I tried using AceDBOptions, and I get the same result.

Am I doing something wrong in here?
Thanks.

myrroddin 09-17-17 05:28 PM

AceDB only writes settings that have been altered to the SavedVariables file. It does not write if nothing has been changed.

In your example, defaultValue has a default of true; you did not change it to false, "Ted", or 007, it is still true. Therefore it is nil in your SV file. Nothing is lost. In fact, it retains the default value from PROFILE_DEFAULTS in memory.

BTW, here's a tip: change this line. This will create a literal profile called Default in the Profiles options tree when presented to the user. You will see what I mean when you load into the game. It makes the life of the user a lot easier.
Code:

self.db = LibStub("AceDB-3.0"):New("MyAddonDB", PROFILE_DEFAULTS, true) -- add true to the end
Also, using SetProfile() as I understand it (could be wrong, but based on your question...) only sets the profile name, it does not set the values of the profile being set. For that, you want CopyProfile(). In any case, if you want people to have one set of values universally upon login, see the code line above.

https://www.wowace.com/projects/ace3...api/ace-db-3-0

myrroddin 09-17-17 06:13 PM

I was giving this some thought. I advise you to not programmatically set profiles for the user. That is really what an addon like Reflux is for. Instead, let the user decide how to manage their profiles.

Again, tack on the word true at the end of that initialization code and see if that is what you wanted.

maqjav 09-18-17 05:47 AM

Hey myrroddin.

Thanks for your help and your time.

I found what the problems was. I needed to add all these handlers to reload my "private.db" value.

Code:

self.db = LibStub("AceDB-3.0"):New("MyAddonDB", PROFILE_DEFAULTS)
self.db.RegisterCallback(self, "OnProfileChanged", "RefreshOptions")
self.db.RegisterCallback(self, "OnProfileCopied", "RefreshOptions")
self.db.RegisterCallback(self, "OnProfileReset", "RefreshOptions")

function MyAddon:RefreshOptions(event, database, newProfileKey)
        private.db = database.profile
end

With this everything works just fine.


All times are GMT -6. The time now is 10:00 PM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI