I have a problem that I cannot solve.
For my UI panel I want a "reset" feature. Thus I have a reset button that can be called to load the database defaults.
The problem I'm having is that as soon as I do this the default data becomes a reference. Thus on a second click on reset it will not work anymore. Because of the reference issue.
Here is my code:
Lua Code:
--object container
local db = CreateFrame("Frame")
ns.db = db
db.default = {}
---------------------------------------------
--DEFAULTS
---------------------------------------------
--default orb setup
db.default.orb = {
--health
["HEALTH"] = {
--filling
filling = {
texture = "Interface\\AddOns\\oUF_Diablo\\media\\orb_filling15",
color = { r = 1, g = 0, b = 0, },
colorAuto = false, --automatic coloring based on class/powertype
},
},--health end
--power
["POWER"] = {
--filling
filling = {
texture = "Interface\\AddOns\\oUF_Diablo\\media\\orb_filling15",
color = { r = 0, g = 0, b = 1, },
colorAuto = false, --automatic coloring based on class/powertype
},
},--power end
} --default end
db.getDefaultOrbHealth = function() return db.default.orb["HEALTH"] end
db.getDefaultOrbPower = function() return db.default.orb["POWER"] end
db.getDefaultOrb = function() return db.default.orb end
---------------------------------------------
--CHARACTER DATA
---------------------------------------------
--load character data defaults
db.loadCharacterDataDefaults = function(type)
if type then
if type == "HEALTH" then
OUF_DIABLO_DB_CHAR[type] = db.getDefaultOrbHealth()
print(addon..": health orb reseted to default")
elseif type == "POWER" then
OUF_DIABLO_DB_CHAR[type] = db.getDefaultOrbPower()
print(addon..": power orb reseted to default")
end
else
OUF_DIABLO_DB_CHAR = db.getDefaultOrb()
print(addon..": character data defaults loaded")
end
db.char = OUF_DIABLO_DB_CHAR
--update the orb view
ns.panel.updateOrbView()
end
OUF_DIABLO_DB_CHAR is my saved variable per character.
Basically when I click the reset button I call
Code:
db.loadCharacterDataDefaults("HEALTH")
I cannot copy the table data via ipairs. Assoc tables cannot be looped. The following will return 0:
Code:
print(# db.default.orb["HEALTH"])
Any idea?
Tuller uses sth like
Lua Code:
local function copyDefaults(tbl, defaults)
for k, v in pairs(defaults) do
if type(v) == 'table' then
tbl[k] = copyDefaults(tbl[k] or {}, v)
elseif tbl[k] == nil then
tbl[k] = v
end
end
return tbl
end
Maybe I'm missing sth...
Oh...his GetDefault funnction is looking like:
Lua Code:
function tullaRange:GetDefaults()
return {
normal = {1, 1, 1},
oor = {1, 0.3, 0.1},
oom = {0.1, 0.3, 1}
}
end
Hmmm...so this could be an idea. I do not create a default table variable in first place that could get referenced later on.
I will try that.
Perfect that did it!
What I'm now doing is:
Lua Code:
--default orb setup
function db:GetOrbDefaults()
return {
--health
["HEALTH"] = {
--filling
filling = {
texture = "Interface\\AddOns\\oUF_Diablo\\media\\orb_filling15",
color = { r = 1, g = 0, b = 0, },
colorAuto = false, --automatic coloring based on class/powertype
},
},--health end
--power
["POWER"] = {
--filling
filling = {
texture = "Interface\\AddOns\\oUF_Diablo\\media\\orb_filling15",
color = { r = 0, g = 0, b = 1, },
colorAuto = false, --automatic coloring based on class/powertype
},
},--power end
} --default end
end