View Single Post
11-05-08, 10:39 AM   #8
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Seerah View Post
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
Code:
print(myDB.foo)
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.

Last edited by Slakah : 11-05-08 at 10:57 AM.
  Reply With Quote