Okay, found a simple example of mine.
In TOC I have nUI_ABGDData located in SavedVariables line
Code:
## SavedVariables: nUI_ABGDData
As a new standard since they implemented it I have the following line at the top of all my addon's LUA files just in case I need global access to shared data. This allows me to split the addon into multiple files which can then be used in multiple addons but still be unique to each addon data wise. For small addons that only use 1 LUA file this shouldn't need to be here but it's a nice easy way to store the addon's name in a variable
Code:
local addonName,addonData = ...
In LUA I have the following near the top of the file. It is similar to your line except I have split the table setup onto 2 lines. This would be the same as writing nUI_ABGDData = { GridDisplay = "auto", } or nUI_ABGDData = { ["GridDisplay"] = "auto", } etc
Code:
--[[ Initialise Saved Variable Table with Default Values ]]--
nUI_ABGDData = {};
nUI_ABGDData.GridDisplay = "auto";
I then have the following set of functions ( which I have changed to only reflect the parts of the code you would need - naming apart )
Code:
--[[ Make changes based on SavedVariables settings ]]--
local function UseSavedVariables(savedVarValue)
if ( savedVarValue == "on" ) then
-- Other settings and actions here
nUI_ABGDData.GridDisplay = "on"
elseif ( savedVarValue == "off" ) then
-- Other settings and actions here
nUI_ABGDData.GridDisplay = "off"
elseif ( savedVarValue == "auto" ) then
-- Other settings and actions here
nUI_ABGDData.GridDisplay = "auto"
end
end
end
And these are the functions I pretty much use near the end of my LUA file that are adjusted to suit the individual addon's requirements but is pretty much the basis of all my addons in some form or another.
Code:
--[[ Check the slash commands ]]--
local function SlashCommands(msg)
-- store both an original version of the message string
-- as well as a lower case version for testing purposes
local origMsg = msg
local lowerMsg = string.lower(msg)
-- convert to lower case and break up arguments
local slashArgs = {};
for word in string.gmatch(lowerMsg,"[^%s]+") do
table.insert(slashArgs,word)
end
-- Set the grid display based on arguments
if ( slashArgs[1] ) then
UseSavedVariables(slashArgs[1]);
else
-- Maybe the users need to know what options are available
end
end
-- Blizzard has finished loading their variables so we can override any changes they may have done to our settings
local function LoadVariables(self,event,...)
-- Set the grid display according to saved variables
UseSavedVariables(nUI_ABGDData.GridDisplay);
-- No need to come here again
self:UnregisterEvent(event);
end
--[[ Our addon variables are loaded so we can do some stuff ]]--
local function AddonLoaded(self,event,...)
-- If not our addon then go away
if ( arg1 ~= addonName ) then return end
-- Set Up the Slash Command Handler
SLASH_MyAddonCmd1 = '/myaddon';
SlashCmdList['MyAddonCmd'] = SlashCommands;
-- Don't need to come back here
self:UnregisterEvent(event)
end
--[[ Handle the OnEvent for the event frame ]]--
local function OnEvent(self,event,...)
if ( event == "VARIABLES_LOADED" ) then
LoadVariables(self,event,...)
elseif ( event == "ADDON_LOADED" ) then
AddonLoaded(self,event,...)
end
end
--[[ Create the Event Frame and Assign its events to watch ]]--
local EventFrame = CreateFrame("Frame","myUniqueEventFrameName",UIParent);
EventFrame:RegisterEvent("VARIABLES_LOADED");
EventFrame:RegisterEvent("ADDON_LOADED");
EventFrame:SetScript("OnEvent",OnEvent);
Hopefully that is enough for you to get your head around things.