Originally Posted by pingumania
and here for the TradeSkillFrame
Lua Code:
if (not TradeSkillFrame) then LoadAddOn('Blizzard_TradeSkillUI') end -- 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.