Quantcast
Defaults "These Settings" handle - WoWInterface
Thread Tools Display Modes
07-29-16, 06:07 PM   #1
Benio
A Murloc Raider
Join Date: Jul 2016
Posts: 8
Defaults "These Settings" handle

Hey, I made an addon that uses Ace3:
It stores options, adds it to Blizzard Addon options page, saves beetween sessions etc.
However, when I click Interface → Options → AddOns → Defaults → These Settings, nothing happens.

Lua Code:
  1. -- libs
  2. -- external /libs as ADDON_NAME.libs objects
  3. ADDON_NAME.libs = {};
  4. ADDON_NAME.libs.Config = LibStub("AceConfig-3.0");
  5. ADDON_NAME.libs.ConfigDialog = LibStub("AceConfigDialog-3.0");
  6. ADDON_NAME.libs.DB = LibStub("AceDB-3.0");
  7. ADDON_NAME.libs.Timer = LibStub("AceTimer-3.0");
Lua Code:
  1. -- options
  2. -- ADDON_NAME.default.global.MODULE_NAME
  3. -- ADDON_NAME.options.args.MODULE_NAME
  4. ADDON_NAME.default = {global = {}};
  5. ADDON_NAME.options = {
  6.     name = "ADDON_FULL_NAME",
  7.     type = "group",
  8.     args = {},
  9. };
Lua Code:
  1. ADDON_NAME.db = ADDON_NAME.libs.DB:New("ADDON_NAMEDB", ADDON_NAME.default, true);
  2. ADDON_NAME.libs.Config:RegisterOptionsTable("ADDON_NAME", ADDON_NAME.options);
  3. ADDON_NAME.frames = {options = ADDON_NAME.libs.ConfigDialog:AddToBlizOptions("ADDON_NAME")};

I tried to implement it according to some comments I saw, using:
Lua Code:
  1. ADDON_NAME.libs.ConfigRegistry = LibStub("AceConfigRegistry-3.0");
Lua Code:
  1. ADDON_NAME.frames.options.default = function(...)
  2.     ADDON_NAME.db = ADDON_NAME.default;
  3.     ADDON_NAME.libs.ConfigRegistry:NotifyChange("ADDON_NAME");
  4. end;

It changes ADDON_NAME.db, and corresponding GUI in options panel at will.
However, it works only first time after (re)loading GUI and changes are not saved.

Problem: How to handle Defaults → "These Settings" to default it's settings and save them correctly?
Any manual page, tutorial, examples or direct solution would be appreciated.

Last edited by Benio : 07-29-16 at 08:36 PM.
  Reply With Quote
07-29-16, 07:00 PM   #2
Ketho
A Molten Giant
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 946
Originally Posted by Benio View Post
Lua Code:
  1. ADDON_NAME.frames.options.default = function(...)
  2.     ADDON_NAME.db = ADDON_NAME.default;
  3.     ADDON_NAME.libs.ConfigRegistry:NotifyChange("ADDON_NAME");
  4. end;

It changes ADDON_NAME.db, and corresponding GUI in options panel at will.
However, it works only first time after (re)loading GUI and changes are not saved.

Problem: How to handle Defaults → "These Settings" to default it's settings and save them correctly?

Tables are passed by reference, what you're doing is just making ADDON_NAME.db point to ADDON_NAME.default instead of copying the table

So anything you do afterwards is with the table referenced in ADDON_NAME.default instead of the dereferenced one from ADDON_NAME.db


If you're lazy you can use Blizzard's CopyTable for deep copying
Lua Code:
  1. function CopyTable(settings)
  2.     local copy = {};
  3.     for k, v in pairs(settings) do
  4.         if ( type(v) == "table" ) then
  5.             copy[k] = CopyTable(v);
  6.         else
  7.             copy[k] = v;
  8.         end
  9.     end
  10.     return copy;
  11. end
Code:
ADDON_NAME.db = CopyTable(ADDON_NAME.default)

But there also was something about AceDB tables being "magic" tables
So in that specific case I'm probably wrong, and I don't really understand that part anymore

http://forums.wowace.com/showthread.php?t=25654#3
http://forums.wowace.com/showthread.php?t=19243
http://www.wowace.com/addons/ace3/pa...-3-0-tutorial/

(Edit) would DBObjectLib:ResetProfile() work for you?
Code:
ADDON_NAME.db:ResetProfile()

Last edited by Ketho : 07-29-16 at 07:32 PM.
  Reply With Quote
07-29-16, 07:47 PM   #3
Benio
A Murloc Raider
Join Date: Jul 2016
Posts: 8
Thanks for a reply! Actually, ResetDB with CopyTable makes change permanent.
Unfortunatelly, it looks like I still need to reload UI in order to make GUI active again (clicking checkbox changes it's visuals, and related ADDON_NAME.db.global setting, but those are not saved on reloading UI).

Lua Code:
  1. ADDON_NAME.db:ResetDB("Default");
  2. ADDON_NAME.db.global = CopyTable(ADDON_NAME.default.global);
  3. ADDON_NAME.libs.ConfigRegistry:NotifyChange("ADDON_NAME");

edit: ResetProfile makes nothing, even with previously RegisterDefaults.

edit2: ResetDB defaults .db already thanks to second parameter of New, so no CopyTable is needed.
Instead, we need to just reinitialize database since it's reset:
Lua Code:
  1. ADDON_NAME.db:ResetDB("Default"); -- reset .db to default settings (passed as second parameter of ADDON_NAME.libs.DB:New)
  2. ADDON_NAME.db = ADDON_NAME.libs.DB:New("ADDON_NAMEDB", ADDON_NAME.default, true); -- reinitialize .db (same as first init above)
  3. ADDON_NAME.libs.ConfigRegistry:NotifyChange("ADDON_NAME"); -- finally, let GUI know to handle changes and work correctly after changing config

Works like a charm.

Final Note: Thanks, Ketho, for pointing out that passing table by reference won't work.
Thanks that, and the linked threads, connected with the "magic" issue, I found out how to handle it.
For futher reference, I am pasting the corrected code:
Lua Code:
  1. -- libs
  2. -- external /libs as ADDON_NAME.libs objects
  3. ADDON_NAME.libs = {};
  4. ADDON_NAME.libs.Config = LibStub("AceConfig-3.0");
  5. ADDON_NAME.libs.ConfigDialog = LibStub("AceConfigDialog-3.0");
  6. ADDON_NAME.libs.ConfigRegistry = LibStub("AceConfigRegistry-3.0");
  7. ADDON_NAME.libs.DB = LibStub("AceDB-3.0");
  8. ADDON_NAME.libs.Timer = LibStub("AceTimer-3.0");
Lua Code:
  1. -- options
  2. -- ADDON_NAME.default.global.MODULE_NAME
  3. -- ADDON_NAME.options.args.MODULE_NAME
  4. ADDON_NAME.default = {global = {}};
  5. ADDON_NAME.options = {
  6.     name = "ADDON_FULL_NAME",
  7.     type = "group",
  8.     args = {},
  9. };
Lua Code:
  1. -- add options
  2. ADDON_NAME.db = ADDON_NAME.libs.DB:New("ADDON_NAMEDB", ADDON_NAME.default, true);
  3. ADDON_NAME.libs.Config:RegisterOptionsTable("ADDON_NAME", ADDON_NAME.options);
  4. ADDON_NAME.frames = {options = ADDON_NAME.libs.ConfigDialog:AddToBlizOptions("ADDON_NAME")};
  5. ADDON_NAME.frames.options.default = function()
  6.     ADDON_NAME.db:ResetDB("Default");
  7.     ADDON_NAME.db = ADDON_NAME.libs.DB:New("ADDON_NAMEDB", ADDON_NAME.default, true);
  8.     ADDON_NAME.libs.ConfigRegistry:NotifyChange("ADDON_NAME");
  9. end;

Last edited by Benio : 07-29-16 at 08:41 PM.
  Reply With Quote
07-29-16, 11:04 PM   #4
Hiketeia
An Aku'mai Servant
 
Hiketeia's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2016
Posts: 33
I was working on the same thing last night- ha!

I did a little example addon for others in case they get stuck too. Not as detailed as your stuff, but might help others get off the ground. It is @ https://github.com/ChrisNolan/TestAd...hor=ChrisNolan
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Defaults "These Settings" handle

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