View Single Post
09-08-08, 01:41 AM   #23
Mikord
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 61
There are definitely some timing consideration to keep in mind, but I'm fairly certain GetSpellInfo is valid before the first addon is loaded.

Also, I know you're just posting pseudocode to a degree, but you should try to avoid using global functions unless you really need to, which in the case of your event callbacks you no longer need to since you are passing them directly via :SetScript instead of calling them globally out of an XML file.

Anyways on to the saved variables.

There really isn't a need to initialize them at the highest level. Just construct your code such that the functions that rely on saved variables aren't called until they are valid. If you're using event callbacks that make use of your SV, then don't register them until after your SV are loaded or initialized via the ADDON_LOADED.

There are many ways to do it, but here is an example of one way.


Code:
local function OnAddonLoaded(self)
 -- Initialize your SV if they don't already exist...
 if not MyModSV then
  MyModSV = {
   showThatSpecialSomething = true,
   anotherSetting = "Yada",
  }
 end

 -- Register your bag updates or do whatever else requires your SV to be loaded.
 self:RegisterEvent("BAG_UPDATE")
end


local function OnBagUpdate(container)
 -- Do whatever on your bag update which you can be sure will
 -- not be called until after you SV are loaded or initialized.
 if MyModSV.showThatSpecialSomething then message("It's special!") end
end


local function OnEvent(self, event, arg1)
 if event == "ADDON_LOADED" then
  if arg1 ~= "MyMod" then return end
  self:UnregisterEvent("ADDON_LOADED")
  OnAddonLoaded(self)

 elseif event == "BAG_UPDATE" then
  OnBagUpdate(arg1)
 end
end

local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("ADDON_LOADED")
eventFrame:SetScript("OnEvent", OnEvent)
  Reply With Quote