View Single Post
02-10-12, 10:48 PM   #13
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Grim, your posts would be 100x more readable if you used paragraphs instead of typing everything in one giant block.

Originally Posted by Grimsin
The bliz LOD's are loading up after player login, curious though do they fire the addon loaded events prior to player entering world?
I have no idea, and it's probably not even consistent between UI loads, let alone between different computers. It's trivially easy to find out for yourself what it's doing on your computer, though:

Code:
local t = {}
local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:RegisterEvent("PLAYER_ALIVE")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function(f, e, ...)
     local m = string.join(", ", e, ...)
     table.insert(t, m)
     print(m)
end)
InterfaceLoadingLog = t
This will print a message to your chat frame every time one of the loading events fires, as well as add them all to a table in the order they fire, so you can check the log later:

Code:
/run for i,v in ipairs(InterfaceLoadingLog) do print(i..". "..v) end
Originally Posted by Grimsin
... my addon code load Bliz LOD stuff in order to start data collection ...
What kind of "data collection" are you doing that requires Blizzard LOD addons to be loaded before they are relevant to the actual user experience?

Originally Posted by Grimsin
... my problem is im trying to use addon.settings.blahblahblah before my addon code has created "addon" or "settings" let alone blahblahblah ...
Any code that needs to reference your addon's saved variables cannot be run before the ADDON_LOADED event has fired for your addon.

Originally Posted by Grimsin
... i have to write out the long way all the stuff that the addon.settings part is handling. which is lots of somthing = {}
No. Instead of inserting default values that probably do not match the actual saved values, just delay running that code until after the ADDON_LOADED event has fired for your addon.

If you are having trouble initializing default settings for your addon, and are not using a library like AceDB-3.0 to handle this, here is a simple method for handling defaults:

Code:
local db

local MyAddon = CreateFrame("Frame")
MyAddon:RegisterEvent("ADDON_LOADED")
MyAddon:SetScript("OnEvent", function(self, event, addon)
	if addon ~= "MyAddon" then return end

	-- Define a table containing a complete configuration
	-- for your addon, with the default values:
	local defaultsTable = {
		blah = "blah",
		etc = "something",
		num = 5,
		height = 200,
		width = 600,
		color = {
			r = 1,
			g = 1,
			b = 0,
			a = 0.5,
			magic = true,
		}
	}

	-- This function goes through your defaults table
	-- and copies to the real settings table any values
	-- that are the wrong type (eg. nil vs string, or
	-- number vs boolean).
	-- It is recursive, so it will go through sub-tables
	-- as well, up to any depth:
	local function copyDefaults(a, b)
		if type(a) ~= "table" then return {} end
		if type(b) ~= "table" then b = {} end
		for k, v in pairs(a) do
			if type(v) == "table" then
				b[k] = copyDefaults(v, b[k])
			elseif type(v) ~= type(b[k]) then
				b[k] = v
			end
		end
		return b
	end

	-- Now call the function, supplying your defaults table
	-- as the source, and your addon's saved variable table
	-- as the destination:
	db = copyDefaults(defaultsTable, MyAddonDB)
	-- Finally, make sure your addon's saved variable points
	-- to the table, in case it did not already exist:
	MyAddonDB = db

	-- Now you can use "db.blah" or "db.color.magic" to get
	-- the current settings. There is no need for any metatables.
end)
  Reply With Quote