AddOn loading process

This technical detail describes the order in which addon code is loaded when the client first starts up, or reloads the interface, as well as what information is available to the Lua environment at various points in the process.

The general outline:
 * 1) When the client first starts, Interface\Addons directory is scanned, and a list of files and addon dependencies is built.
 * 2) Addon code is executed after the player selects a character.
 * 3) After all addon code has been loaded, the saved variables are executed. ADDON_LOADED fires after each addon's SVs have been loaded.

The initial scan and addon loading order
The client scans through the Interface\AddOns directory, storing a list of present files, and loading all .toc files into memory; files that were not found during this step cannot be loaded by the game later. This step only happens on client start-up, which makes it impossible to install additional addons / update .toc files / add additional graphics and/or data files to addons without restarting the client.

Dependency information in the .toc files is used to compute the order in which addons will be loaded when the player logs in. In most cases, an individual addon may assume that all of the addons that it depends on will be loaded first.

Files within an addon
Files within an addon are loaded in the order they're listed in the addon's .toc file. Files included through XML  or  are loaded at the time the tag is encountered while parsing. XML OnLoad script handlers execute when all of a widget's children have been created.

When the addon code is first loaded during this step, only basic information about the player -- name, class, race and realm -- is available.

To illustrate the loading order, consider the following addon code example:

LoadingOrder.toc file1.lua file2.xml file3.lua file1.lua print("This loads first") file2.xml   print("Inherited elements of the frame are processed first.");      print("First child frame's OnLoad fires first")</OnLoad></Scripts> </Frame>  print("Second child frame's OnLoad fires second")</OnLoad></Scripts> </Frame> 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 print("This concludes this presentation");
 * 1) Interface: 30000
 * 2) Title: Loading Order Demo

Saved variables loading
After the addon code has been loaded, the loading process can be followed by registering for various events, listed here in order of firing.
 * 1) ADDON_LOADED
 * 2) * This event fires whenever an AddOn has finished loading and the SavedVariables for that AddOn have been loaded from their file.
 * 3) SPELLS_CHANGED
 * 4) * This event fires shortly before the PLAYER_LOGIN event and signals that information on the user's spells has been loaded and is available to the UI.
 * 5) PLAYER_LOGIN
 * 6) * This event fires immediately before PLAYER_ENTERING_WORLD.
 * 7) * Most information about the game world should now be available to the UI.
 * 8) * All Sizing and Positioning of frames is supposed to be completed before this event fires.
 * 9) * AddOns that want to do one-time initialization procedures once the player has "entered the world" should use this event instead of PLAYER_ENTERING_WORLD.
 * 10) PLAYER_ENTERING_WORLD
 * 11) * This event fires immediately after PLAYER_LOGIN
 * 12) * Most information about the game world should now be available to the UI. If this is an interface reload rather than a fresh log in, talent information should also be available.
 * 13) * All Sizing and Positioning of frames is supposed to be completed before this event fires.
 * 14) * This event also fires whenever the player enters/leaves an instance and generally whenever the player sees a loading screen
 * 15) PLAYER_ALIVE
 * 16) * This event fires after PLAYER_ENTERING_WORLD
 * 17) * Quest and Talent information should now be available to the UI

Until 3.0, VARIABLES_LOADED used to fire upon completion of the addon loading process; since 3.0, it is fired in response to CVars, Keybindings and other associated "Blizzard" variables being loaded, and may therefore be delayed until after PLAYER_ENTERING_WORLD. The event may still be useful to override positioning data stored in layout-cache.txt

Load On Demand behavior
Load on Demand addons cannot rely on most of the event sequence being fired for them; only ADDON_LOADED is a reliable indication that the saved variables for your LoD addon have been loaded.