View Single Post
08-24-15, 07:40 AM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by pingumania View Post
and here for the TradeSkillFrame
Lua Code:
  1. if (not TradeSkillFrame) then
  2.     LoadAddOn('Blizzard_TradeSkillUI')
  3. end
  4. -- stuff happens here
But still I wonder if this is a good way to do it.
That isn't; you should avoid forcibly loading any Blizzard addons, as they're not designed to handle that and you may cause other problems. A better way:

Code:
local function setupTradeSkillUI()
   -- stuff happens here
end
if TradeSkillFrame then
   setupTradeSkillUI()
else
   local f = CreateFrame("Frame")
   f:RegisterEvent("ADDON_LOADED")
   f:SetScript("OnEvent", function(f, event, name)
      if name == "Blizzard_TradeSkillUI" then
         setupTradeSkillUI()
      end
   end)
end
If you end up needing to wait for multiple addons to load, you can do it all from one frame:
Code:
local setupFuncs = {}

-- Blizzard_TradeSkillUI
tinsert(setupFuncs, function()
   if not TradeSkillFrame then
      return true
   end
   -- do your stuff here
end)

-- Some other addon
tinsert(setupFuncs, function()
   if not MagicalUnicornsFrame then
      return true
   end
   -- do stuff with MagicalUnicornsFrame and/or the addon that created it
end)

local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("OnEvent", function()
   for i = #setupFuncs, 1, -1 do
      local tryAgain = setupFuncs[i]()
      -- If it returned true, keep it and try again on the next event,
      -- otherwise it's done, so remove it from the table.
      if not tryAgain then
         tremove(setupFuncs, i)
         -- ^ Removing entries as we go is why we're looping backwards.
      end
   end
end)
I use this tactic in a few personal addons (border, general mods, etc.). The reason for using a simple list (indexed table) instead of using addon names as table keys is that not all addons are ready to hook/modify as soon as they load. Depending on what addons you're waiting for, you may need to listen for additional events to detect when they're ready to do stuff with.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote