API:SaveVariables

__NOTOC__
Registers a variable to be saved automatically after the SAVE_VARIABLES event is triggered. The variable is automatically loaded before the VARIABLES_LOADED event is triggered.
SaveVariables("variableName")
NOTE: You must call this function every time your addon is loaded, not when the variable's contents change. This function requests that the game save the specified variable on exit, it is not a request for the game to capture the current value and save it. You should always call this from your OnLoad callback. If you fail to call this each time your addon loads, your value will likely get erased.
== Parameters ==
=== Arguments ===
:;variableName
::String - The name of your variable. NOT the actual variable. Note that it doesn't save functions, threads, or userdata.
=== Returns ===
:;none
== Example ==
function MyAddon:OnLoad(self)
 -- Register the events that need to be watched
 self:RegisterEvent("VARIABLES_LOADED")
 self:RegisterEvent("SAVE_VARIABLES")
 -- Set the Variable to be saved (and consequently loaded too)
 SaveVariables("MyAddon_SaveVariablesSettings")
end
function MyAddon:OnEvent(event, ...)
 MyAddon[event](...)
end
function MyAddon:VARIABLES_LOADED(...)
 --Apply Loaded Variables to MyAddons's settings, or use the default settings if Loaded Variables are nil or false
 MyAddon:Settings = MyAddon_SaveVariablesSettings or MyAddon_DefaultSettings
 MyAddon_SaveVariablesSettings = MyAddon:Settings
end
function MyAddon:SAVE_VARIABLES(...)
 --Last Chance to set stuff in MyAddon_SaveVariablesSettings
 MyAddon_SaveVariablesSettings.Anchor = {}
 MyAddon_SaveVariablesSettings.Anchor.point,
 MyAddon_SaveVariablesSettings.Anchor.relativePoint,
 MyAddon_SaveVariablesSettings.Anchor.relativeTo,
 MyAddon_SaveVariablesSettings.Anchor.offsetX,
 MyAddon_SaveVariablesSettings.Anchor.offsetY = MyAddonFrame:GetAnchor()
 MyAddon_SaveVariablesSettings.Anchor.relativeTo = MyAddon_SaveVariablesSettings.Anchor.relativeTo:GetName()
end
== Notes ==
* The SAVE_VARIABLES event is triggered when the game:
:1) exits gracefully.
:2) returns to the character selection screen.
* The VARIABLES_LOADED event is triggered when the game:
:1) Loads.
:2) Reloads the UI.
:3) Possibly on a Zone Change (Needs testing)
* Variables will not get saved if there is:
:- a client crash.
:- a ReloadUI.
:- a MS Window's End Process.
* Saved Variables are not appropriate for maintaining persistent states through ReloadUI(). This is because the game overwrites the saved variables after every ReloadUI, but fails to save the variables before a ReloadUI(). However, since memory is persistent through a ReloadUI(), you can assign the saved variable to another variable which doesn't get overwritten.
* Variables declared as local will not be saved, as they are (obviously) local to your addon's scope.
== Related Functions ==
* SaveVariablesPerCharacter
* LoadVariablesAnchor
* SaveVariablesAnchor
Category:Functions