It's actually suggested that you have a table of values saved instead of a saved variable for each one. This will reduce the exposure of your addon to the global namespace. The reason keeping the variable scope of your addon tight is important is not only because using locals is faster than globals, but without careful naming of your globals can cause a lot of problems and even break your addon (and others) due to what is known as global leaks.
With that out of the way, here's an example of using a saved configuration table.
Note: This melds the ability of saved variables with the performance of a local.
ToC:
Code:
##Interface: 60000
##Title: MyAddon
##Notes: Description
##SavedVariables: MyAddonConfig
Lua Code:
local Name=...;-- This will retrieve our Addon ID
-- Default configuration
local Config={-- This will be our upvalue where we're going to access our configuration table
QWindowVisible=true;
QWindowEnabled=true;
}
MyAddonConfig=Config;-- We'll load our default into the global pointed to by our ToC
local MyFrame=CreateFrame("Frame");
MyFrame:RegisterEvent("ADDON_LOADED");
MyFrame:SetScript("OnEvent",function(self,event,...)
if event=="ADDON_LOADED" and (...)==Name then-- Check if our addon is the one that loaded
-- If there is any saved data, it'll overwrite whatever is in the global
Config=MyAddonConfig;-- Resync upvalue to saved data (defaults will remain if no data loaded)
-- Now we can use our Config upvalue as if it were our saved variable
MyAddonQWindow:SetShown(Config.QWindowVisible);
MyAddonQWindow:EnableMouse(Config.QWindowEnabled);
self:UnregisterEvent("ADDON_LOADED");-- We no longer need this event, unregister it
end
end);