View Single Post
11-25-14, 08:47 PM   #17
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
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:
  1. local Name=...;--   This will retrieve our Addon ID
  2.  
  3. --  Default configuration
  4. local Config={--    This will be our upvalue where we're going to access our configuration table
  5.     QWindowVisible=true;
  6.     QWindowEnabled=true;
  7. }
  8. MyAddonConfig=Config;-- We'll load our default into the global pointed to by our ToC
  9.  
  10. local MyFrame=CreateFrame("Frame");
  11. MyFrame:RegisterEvent("ADDON_LOADED");
  12. MyFrame:SetScript("OnEvent",function(self,event,...)
  13.     if event=="ADDON_LOADED" and (...)==Name then-- Check if our addon is the one that loaded
  14. --      If there is any saved data, it'll overwrite whatever is in the global
  15.         Config=MyAddonConfig;-- Resync upvalue to saved data (defaults will remain if no data loaded)
  16.  
  17. --      Now we can use our Config upvalue as if it were our saved variable
  18.         MyAddonQWindow:SetShown(Config.QWindowVisible);
  19.         MyAddonQWindow:EnableMouse(Config.QWindowEnabled);
  20.  
  21.         self:UnregisterEvent("ADDON_LOADED");-- We no longer need this event, unregister it
  22.     end
  23. end);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote