Quantcast AceDB OnInitialize does not save - WoWInterface
Thread Tools Display Modes
10-15-19, 12:24 AM   #1
paladinmaladin
A Defias Bandit
Join Date: Oct 2019
Posts: 2
AceDB OnInitialize does not save

I'm having some difficulties in getting AceDB to save changes between sessions. I have a small core addon, a separate module addon to go along with it, and an AceConfigDialog to change settings.

When changing settings within the session everything works as expected; however, those changes don't persist between sessions (logout, ReloadUI, etc.). If I move setting up the db to the ADDON_LOADED event, the changes are saved between sessions. Hopefully someone can help me out in explaining why this is?

Core.lua:
Code:
aUI = LibStub("AceAddon-3.0"):NewAddon("aUI", "AceConsole-3.0", "AceEvent-3.0")
local db

aUI.Version = {
    [1] = 2,
    [2] = 0,
    [3] = " alpha",
}

local defaults = {
    profile = {
        ['*'] = true,
        modules =  {
            ErrorControl = true,
        },
        test = {
            val1 = false,
            val2 = false,
            val3 = true,
        }
    }
}

aUI.optTable = {
    --big options table
}

LibStub("AceConfig-3.0"):RegisterOptionsTable("aUI", aUI.optTable, "o")
LibStub("AceConfigDialog-3.0"):AddToBlizOptions("aUI", "aUI Test")

function aUI:SetModuleState(mod, value)
    if value ~= db.modules[mod] then
        db.modules[mod] = value

        if value then
            self:EnableModule(mod)
        else
            self:DisableModule(mod)
        end
        self.db.profile = db
    end
end

-- Get Module State
function aUI:GetModuleState(mod)
    return db.modules[mod]
end

-- Get Option Tables
function aUI:UpdateOptions(...)
    if (...) then
        local name, options = ...
        local mod = aUI:GetModule(name)
        aUI.optTable.args[name] = options
    else
        for name, mod in aUI:IterateModules() do
            optTable.args[name] = mod:GetOptionsTable()
        end    
    end
end

function aUI:ADDON_LOADED()
    -- this works
    -- self.db = LibStub("AceDB-3.0"):New("aUIDB", defaults, true)

    -- db = self.db.profile
end

function aUI:OnInitialize()
    --Setup DB
    self.db = LibStub("AceDB-3.0"):New("aUIDB", defaults, true)

    db = self.db.profile

    --Register Chat Commands
    self:RegisterChatCommand("rl", function() ReloadUI() end)

    self:RegisterEvent("ADDON_LOADED")
end
Module.lua:
Code:
local aUI = LibStub("AceAddon-3.0"):GetAddon("aUI")

local MODULE = "ErrorControl"
local ErrorControl = aUI:NewModule(MODULE, "AceEvent-3.0")
local db

-- List of UI Errors
local FilterList = {
    -- Long ipairs of [UI_ERRORS] = UI_ERRORS
}

function ErrorControl:UI_ERROR_MESSAGE(event, errCode, err)
    if err == "" then return end

    if not db.filterList[err] and not db.hideAllErrors then
        UIErrorsFrame:AddMessage(err, 1, 0, 0)
    end
end

function ErrorControl:OnInitialize()
    self.db = aUI.db:RegisterNamespace(MODULE)
    self.db:RegisterDefaults({
        profile = {
            hideAllErrors = false,
            filterList = {
                -- List of default [UI_ERRORS] = true/false
            },
        },
    })
    db = self.db.profile

    self:SetEnabledState(aUI:GetModuleState(MODULE))
    aUI:UpdateOptions(MODULE, self:GetOptionsTable())
    self:RegisterEvent("ADDON_LOADED")
end

function ErrorControl:OnEnable()
    self:RegisterEvent("UI_ERROR_MESSAGE")
    UIErrorsFrame:UnregisterEvent("UI_ERROR_MESSAGE")
end

function ErrorControl:OnDisable()
    self:UnregisterEvent("UI_ERROR_MESSAGE")
    UIErrorsFrame:RegisterEvent("UI_ERROR_MESSAGE")
end

function ErrorControl:GetOptionsTable()
    local options = {
        -- long options table using FilterList for multiselect
    }

    return options
end
  Reply With Quote
10-15-19, 04:14 AM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,057
Check out SmartRes2 and SmartRes2 Chat Module to see a working example.

Both addons are "dead" for Retail and Classic, the former because it isn't needed, the latter because I'd have to rewrite they thing (yet again) from scratch including LibResInfo. However, the parts you want do still work.
  Reply With Quote
10-15-19, 05:18 PM   #3
paladinmaladin
A Defias Bandit
Join Date: Oct 2019
Posts: 2
Thanks, I took a look - but I don't see anything different? Maybe I'm just blind.. AceDB is created OnInitialize() and in the module gets a reference to the DB object and registers a namespace, setting defaults.
  Reply With Quote
10-19-19, 04:28 PM   #4
jadiilelz
A Defias Bandit
Join Date: Oct 2019
Posts: 2
Old thread but wanted to chime in anyhow.

I have looked around for quite a while for a tutorial that is not only concise but is actually 'complete'. I think I may have found some good stuff to share with you or whomever else may be wondering why in the fuck after 15 years, there is still no legitimate documentation online.

https://wow.gamepedia.com/WelcomeHom...rst_Ace3_Addon
https://wow.gamepedia.com/Saving_var...ommon_Pitfalls

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » AceDB OnInitialize does not save

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