WoW:Saving variables between game sessions: Difference between revisions

Clarified load sequence.
m (→‎VARIABLES_LOADED event: minor rewording)
(Clarified load sequence.)
Line 7: Line 7:
* <tt>WTF\Account\ACCOUNTNAME\RealmName\CharacterName\AddOnName.lua</tt> - Per-character settings for each individual AddOn
* <tt>WTF\Account\ACCOUNTNAME\RealmName\CharacterName\AddOnName.lua</tt> - Per-character settings for each individual AddOn


These files are written whenever the UI engine shuts down (either at game quit, or at the start of a forced UI reload), and then executed when the UI is started and addons are loaded (after logging in or at the end of a forced UI reload).
These files are written whenever the UI engine shuts down, either when the game is quit, or when you force a UI reload (with <code>/console reloadUI</code>). Like the other .lua scripts for the UI, the SavedVariables scripts are executed when the UI is started and AddOns are loaded. This happens when you first log on, and each time you force a UI reload.


The <tt>SavedVariables.lua</tt> files are executed as code (just like your addon code). The time at which they're executed varies slightly by file type -- The AddOn specific SavedVariables files are executed right after the addon they belong to has been loaded, and before the ADDON_LOADED event is fired for the addon. The global SavedVariables.lua file is loaded after all of the initial addons are loaded, before the player is given control, and before the VARIABLES_LOADED event is fired.
The SavedVariables files are executed at different times, depending on their types, and it's important to understand the when they're executed, particularly relative to the <code>ADDON_LOADED</code> and <code>VARIABLES_LOADED</code> events. The sequence is as follows:
* MyAddOn loads
* AddOnName.lua loads and executes
* ADDON_LOADED event fires for MyAddOn
* &hellip; other AddOns load &hellip;
* SavedVariables.lua loads and executes
* VARIABLES_LOADED event fires
* Player gets control


When the UI engine shuts down, only those variables which have been marked for saving will be written into the <tt>SavedVariables.lua</tt> files. A variable can be written to more than one file (though this is generally ill-advised since it can cause settings to be overwritten later on)
When the UI engine shuts down, only those variables which have been marked for saving will be written into the <tt>SavedVariables.lua</tt> files. A variable can be written to more than one file (though this is generally ill-advised since it can cause settings to be overwritten later on)
Anonymous user