View Single Post
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