WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Defaults "These Settings" handle (https://www.wowinterface.com/forums/showthread.php?t=54065)

Benio 07-29-16 06:07 PM

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.

Ketho 07-29-16 07:00 PM

Quote:

Originally Posted by Benio (Post 317186)
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()

Benio 07-29-16 07:47 PM

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;

Hiketeia 07-29-16 11:04 PM

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


All times are GMT -6. The time now is 12:07 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI