I'm not certain but I don't think variables are loaded at onload, also metatables are not copied to the WTF file so "getmetatable(listDB) == nil" will always evaluate false.
setmetatable(myVar or {}, {__index = default}) is used to apply defaults, so when you try to index a key with a nil value the default will be used instead.
i.e.
Code:
local default = {foo = "bar"}
local t = setmetatable({}, {__index = default})
print(t.foo)
would print "bar", because the t table is empty, so it falls back to the index entry of default.
Just doing
this:RegisterEvent("ADDON_LOADED");
|
would not do anything as you don't have an event handler.
So if you want a run of the mill Saved Variable handler (this is done using purely lua, as I don't like XML).
you could do:
Code:
local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
local list
local function OnEvent(self, event, addon)
if addon ~= "myaddon" then return end
if not MyVar then
MyVar = {} --If MyVar is empty hand it a table
end
list = MyVar
self:UnregisterEvent("ADDON_LOADED") --stop ADDON_LOADED from firing for this frame
OnEvent = nil -- Clean up OnEvent for Garbage Collection
end
f:SetScript("OnEvent", OnEvent)
Hope this helps.