Originally Posted by Seerah
When I add a new variable to be saved after MYVAR has been created, MYVAR doesn't pick it up. I get errors because even though it's defined in the defaults table, the addon is looking for it in MYVAR. Which is the best way to go about this?
|
You can use metatables to set a default value if no value for that key is found.
i.e.
Code:
local defaults = {
foo = "bar",
tbl = {
cat = "meow",
dog = "bark",
cow = "moo"
}
}
local myDB = setmetatable(myDB or {}, {__index = defaults})
so if I were to do
then it would output "bar", and
Code:
print(myDB.tbl.cow)
would output "moo".
but then if I were to do
Code:
myDB.tbl.cow = "asleep"
then
Code:
print(myDB.tbl.cow)
would output "asleep".
Hope this helps.