AddOn loading process

This page details how AddOns load and unload, including the order when information becomes available such as SavedVariables and character details.
==AddOn loading order==
Blizzard's AddOns usually load first (the native UI), followed by custom AddOns in alphabetical order. However, .toc directives can override this behaviour (e.g. LoadOnDemand or LoadWith).
Within an AddOn:
* XML and Lua files load sequentially as listed in the .toc file, and also when reaching or tags in XML.
* Saved variables load after the last file, overwriting any default values set earlier.
* Finally, fires to indicate the AddOn has finished loading. This is the earliest time for an AddOn to read its saved variables.
==Events during login==
AddOns load in parallel to the login screen, but can register for events to access information as it becomes available. However, LoadOnDemand AddOns might trigger part-way or after all steps have finished:
Normal sequence:
# 
#* Fires when each AddOn finishes loading, including all files and saved variables.
#* Confirm which AddOn loaded using the first argument (in case a LoadOnDemand AddOn loaded in parallel), access saved variables, and deregister from ADDON_LOADED to avoid repeated callbacks.
# 
#* Fires during the loading screen when logging in or reloading, before the first . Most information about the game world is available to the UI.
#* Finish any one-time initialization and frame placement, so they are in place when the load screen disappears.
# 
#* Fires during any loading screen including logging in, reloading or entering an instance. The first time happens after . If this is a reload, talent information is also available.
#* Finish any reoccuring (ie, instance-dependent) initialization and frame placement, so they are in place when the load screen disappears.
# 
#* Fires when the player is entering combat lockdown. If the player is in combat while reloading, it will follow .
#* Finish any outstanding modifications to secure frames, as they are unalterable during combat lockdown.
# 
#* Fires when spells in the spellbook is populated, and again when it changes. Currently, this event fires after the preceeding events (but it was not always this way).
#* Update any pertinent non-secure frames.
;Error condition:
: 
:* Fires when the client runs out of memory while attempting to load saved variables, after .
:* Usually there is no further action; the client displays a popup informing the user of the error.
==Events during logout==
#
#* Does not imply logout, as it may preceed another loading screen (instances, continents, etc.).
#
#* Last chance to modify SavedVariables before AddOns unload.
#
#* All AddOns commit SavedVariables prior to reloading, quitting or logging off. Called once only, rather than for each AddOn. This does not occur during a crash or Alt-F4.
==Example==
LoadingOrder.toc
 ##Interface: 
 ##Title: Loading Order Demo
 ##SavedVariables: LoadingOrderSavedVar
 file1.lua
 file2.xml
 file3.lua
file1.lua
print("This loads first")
file2.xml
 <Ui>
 <Frame name="TemplateFrame" virtual="true">
 <Frames><Frame><Scripts><OnLoad>print("Inherited elements of the frame are processed first.") </OnLoad></Scripts></Frame></Frames>
 </Frame>
 <Frame inherits="TemplateFrame">
 <Frames>
 <Frame>
 <Scripts><OnLoad>print("First child frame's OnLoad fires first")</OnLoad></Scripts>
 </Frame>
 <Frame>
 <Scripts><OnLoad>print("Second child frame's OnLoad fires second")</OnLoad></Scripts>
 </Frame>
 <Scripts><OnLoad>print("Parent frame's OnLoad fires after its children's.")</OnLoad></Scripts>
 </Frames>
 </Frame>
 <Script file="file2.5.lua"/>
</Ui>
file2.5.lua
print("Files included in XML are executed as they are encountered")
file3.lua
LoadingOrderSavedVar = 0 -- default value until ADDON_LOADED
print("The last file has loaded.")
local frame = CreateFrame("Frame")
frame:RegisterFrame("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event)
 if event == "ADDON_LOADED" and arg1="LoadingOrder" then
 LoadingOrderSavedVar = LoadingOrderSavedVar + 1
 if LoadingOrderSavedVar then
 print("The demo has now loaded .." LoadingOrderSavedVar .. " times".)
 end
 end
end)
==Patch changes==
* 
* 
* 
==See also==
* Handling events for information on how to sign up to receive event notifications
==References==