WoW:Saving variables between game sessions: Difference between revisions

m
→‎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 initialized them with table values without paying attention to VARIABLES_LOADED event. Because, as explained in loading sequence above, file with saved variables is processed after code of your addon and because entire table is single value, if you define table with default values like this:
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 have no 'key2' will overwrite table defined in source and, therefore, 'key2' won't have default value set.
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.


Correct way to initialize default values is to register for VARIABLES_LOADED event, test keys that you want to initialized if they are nil or not and write your default values.
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 number or strings, still can be initialized without waiting for 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 saved file and all you work will be overwritten after that), but for sake of simple maintenance in case your initialization becomes more complex in future, it is advised to use registering for VARIABLES_LOADED event from beginning to save you rewrite or headache of catching elusive bugs later.
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]]
Anonymous user