Quantcast WoWInterface - View Single Post - Better Saved Variables Tutorial?
View Single Post
11-06-08, 01:43 AM   #14
conner686
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 4
Originally Posted by slizen View Post
Just checking your db table with:
Code:
MYMODDB = MYMODDB or defaults
is not enough.
The problem is it only checks if the table exists, not that all keys that are in the default table also exist in the MYMODB table.


So even if you add for example an alpha key to your defaults table, the table loaded from the saved variables where there is no alpha key will still be used.

Something like this should work:
Code:
for k,v in pairs(defaults) do
    if type(MYMODDB[k]) == "nil" then
        MYMODB[k] = v
    end
end
That or using a metatable as Slakah said.
I use this method, and it provides *no* protection if performed within the VARIABLES_LOADED event. My solution was gross, involving a timed event one second after VARIABLES_LOADED. I guess you can also use a one-shot PLAYER_ENTERED_WORLD event, but that didn't occur to me at the time.

That metatable thing looks *amazing.* I wish I hadn't been intimidated by the underscores when I first stumbled upon the concept, it looks like quite the ideal solution.
  Reply With Quote