WoW:Saving variables between game sessions

From AddOn Studio
Revision as of 18:19, 4 January 2005 by WoWWiki>Flickering (First stab at this one)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

(Note - this is just a quick first version, hopefully someone can make it nicer. Flickering 13:19, 4 Jan 2005 (EST))

SavedVariables.lua

With the exception of a few game-managed properties (slot contents, graphics settings, etc), all custom values which need to be saved between sessions are maintained in a single file, SavedVariables.lua, which is 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 (after logging in or at the end of a forced UI reload).

Everything that's in that file is always loaded at start up, but only selected global variables are written at the end. There are a couple of ways to indicate that a variable should be saved:

SavedVariables in AddOn's .toc

The best way is to use the SavedVariables keyword in your Addon's .toc file. If you have to store lots of pieces data, or data with dynamic keys, wrap it in one or two table variables rather than consuming huge quantities of global namespace.

This method is best because it works even if your module is not active (based on the ui addons menu), has a version mismatch, or fails during startup due to compilation, dependency, or other issues.

The RegisterForSave function

LUA code can explicitly request that a variable be saved by calling the RegisterForSave("varName") function with the variable name that should be saved. That variable then becomes flagged for saving at the end of the session. It's important to note that since this is an active process, the variable will only be saved if it is registered during a session, so compilation execution failures that prevent the call from being made will end up without the variable being flagged.

Before the addition of the SavedVariables section in the .toc this was the only way to flag variables for saving, however now use of this function is not recommended unless there's a specific reason for optional saving (and you understand all the risks).

IMPORTANT: "Dynamic variable names" is NEVER a reason to use this function, use a table instead with dynamic keys, i.e.:

 ADDON_SETTINGS = {}
 ADDON_SETTINGS["dynamic" .. something] = value

Settings are per-ACCOUNT

It's important to note that settings are saved per-account, not per-character. If you wish to do per-character settings in your AddOn then you should use something like the player name as a key into a table for settings. This does give the AddOn author some flexibility in being able to make some settings global, some per-class, some per-user, etc.