EVENT_ADD_ON_LOADED fires when your addon has finished loading and your previously saved variables are ready to be accessed. Assume this is your addon below - I'll comment the order in which everything occurs.
YourAddon.txt
Code:
## Title: YourAddon
YourAddon.lua --1: this file and its contents gets loaded into memory first
YourAddon.xml --6: now that the above is finished, this file gets executed
YourAddon.lua - this file is loaded into memory from top to bottom
Lua Code:
local myVar, counterLabel --2: these local variables exist in memory now but haven't been assigned anything yet (nil)
--3: this function is loaded into memory but hasn't actually been called/run yet
local function YourAddonInitialize()
EVENT_MANAGER:RegisterForEvent("YourAddon", EVENT_PLAYER_DEAD, YourAddonEvent) --12: this other event gets registered for
counterLabel = WINDOW_MANAGER:CreateControl("YourAddonFrameCounter", YourAddonFrame, CT_LABEL) --13: the label is now created (meanwhile, the OnUpdate script has tried to access it multiple times)
myVar = 0 --14: the counter is set to its initial value
end
--4: the event is registered for, but hasn't fired yet, and so still hasn't called the function
EVENT_MANAGER:RegisterForEvent("YourAddon", EVENT_ADD_ON_LOADED, function(event, addon)
if addon == "YourAddon" then --11: your addon and its saved variables have finished loading, so EVENT_ADD_ON_LOADED fires and calls the initialize function since the addon argument matches the name of our addon now
YourAddonInitialize()
end
end)
--5: this function is loaded into memory but hasn't been called yet
function YourAddonUpdate() --9...: this begins to be called constantly starting with when the handler is set at #8 below
YourAddonFrameCounter:SetText(myVar) --note it's trying to access the counter which still hasn't been created yet
myVar = myVar + 1
end
YourAddon.xml - this file is executed from top to bottom
Code:
<GuiXml>
<Controls>
<TopLevelControl name="YourAddonFrame"> --7: your toplevel window is created
<Dimensions x="200" y="42" />
<Anchor point="CENTER" />
<OnUpdate>
YourAddonUpdate() --8: your OnUpdate handler is set to this function - this now begins firing and calls this function according to your frame rate (50 times per second, if at 50fps)
</OnUpdate>
<Controls>
<Backdrop name="$(parent)BG" inherits="ZO_ThinBackdrop" /> --10: the bg frame is created
</Controls>
</TopLevelControl>
</Controls>
</GuiXml>
Now... this isn't the exact format that you described your addon to be, but am I close? Do you see the error(s)?