[ADDON BUG] Sub-table in Saved Variables

#0 - April 12, 2007, 11:46 a.m.
Blizzard Post
An addon's TOC file seems to allow using a sub-table in the SavedVariables list, but an error occurs when loading it because the primary table is never created prior to creating the sub-table.

By sub-table, I mean something to the effect of Foo.Bar = {} (where Bar is the sub-table of Foo).


The following should help to demonstrate this bug:


Addon.toc

## Interface: 20003
## Title: My Addon
## SavedVariables: Foo.Bar
Addon.lua


Addon.lua

if not Foo then Foo = {}; end

Foo.LoadAddon = function()
if not Foo.Bar then Foo.Bar = {}; end
Foo.Bar.TestVar = "Test me!"
end

Foo.OnEvent = function()
if event == "ADDON_LOADED" and arg1 == "Addon" then
Foo.LoadAddon()
end
end

local frame = CreateFrame("Frame")
frame:SetScript("OnEvent", Foo.OnEvent)
frame:RegisterEvent("ADDON_LOADED")




Reload the UI to load the newly-created saved variables file:

SavedVariables\Addon.lua

Foo.Bar = nil



And, finally, the error message is revealed:
WTF\Account\<Name>\SavedVariables\Addon.lua:2: attempt to index global 'Foo' (a nil value)


The problem is that the saved variables script is trying to create Foo.Bar when Foo doesn't yet exist (it's nil). So when creating this file, it'll need to look through and create the other tables.


Foo = {
Bar = nil
}


And then a more complicated structure like Addon.Table.Something.Else would be:


Addon = {
Table = {
Something = {
Else = nil
}
}
}
#2 - April 13, 2007, 4:38 p.m.
Blizzard Post
Foo.Bar isn't legal saved variable syntax. It looks the entire name up in the global namespace and saves that.

e.g.
print _G["Foo.Bar"] will always yield nil ... unless you're doing some really wacky things. :)