WoW:Saving variables between game sessions (source)
Revision as of 17:32, 14 December 2007
, 14 December 2007→VARIABLES_LOADED event: Improved readability.
(Clarified load sequence.) |
m (→VARIABLES_LOADED event: Improved readability.) |
||
| Line 101: | Line 101: | ||
== VARIABLES_LOADED event == | == VARIABLES_LOADED event == | ||
When working with variables designated for saving, you should not | When working with variables designated for saving, you should not initialize them with table values without paying attention to the VARIABLES_LOADED event. As explained in the loading sequence above, the file with saved variables is processed after the code of your addon. Since the entire table is a single value, if you define a table with default values like this: | ||
tableName = { | tableName = { | ||
['key1'] = 'value1'; | ['key1'] = 'value1'; | ||
| Line 110: | Line 110: | ||
['key2'] = 'value2'; | ['key2'] = 'value2'; | ||
} | } | ||
when your saved variables are loaded, saved value of table that | when your saved variables are loaded, the saved value of a table that has no 'key2' will overwrite the table defined in source. That causes 'key2' to have no set default value. | ||
The correct way to initialize default values is to register for the VARIABLES_LOADED event. You can then test keys that you want initialized to see if they are nil or not and write your default values. | |||
function AddOnName_OnLoad() | function AddOnName_OnLoad() | ||
| Line 125: | Line 125: | ||
end | end | ||
Simple values, such as | Simple values, such as numbers or strings, still can be initialized without waiting for the event if you do not require any additional checks or processing (because you'd be working on your hard-coded values, not on those read from the saved file and all your work will be overwritten after that). For the sake of simple maintenance in case your initialization becomes more complex in the future, it is advised to use registering for the VARIABLES_LOADED event from beginning to save you a rewrite or headache of catching elusive bugs later. | ||
[[Category: HOWTOs|Save Variables Between Game Sessions]] | [[Category: HOWTOs|Save Variables Between Game Sessions]] | ||