Thread Tools Display Modes
09-17-17, 02:10 PM   #1
maqjav
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Feb 2012
Posts: 60
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.

Last edited by maqjav : 09-17-17 at 02:16 PM.
  Reply With Quote
09-17-17, 05:28 PM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
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

Last edited by myrroddin : 09-17-17 at 05:30 PM. Reason: grammar
  Reply With Quote
09-17-17, 06:13 PM   #3
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
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.
  Reply With Quote
09-18-17, 05:47 AM   #4
maqjav
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Feb 2012
Posts: 60
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.

Last edited by maqjav : 09-18-17 at 05:51 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » AceDB lost default values on SetProfile

Thread Tools
Display Modes

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