WoW:Saving variables between game sessions: Difference between revisions

m
m (→‎Annoyances: This is the last time, I swear...)
m (→‎VARIABLES_LOADED event: minor rewording)
Line 92: Line 92:
  savedVar = f()  -- will fail to save g
  savedVar = f()  -- will fail to save g


== Annoyances ==
== VARIABLES_LOADED event ==


If you save a table like this:
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:
  tableName = {
  tableName = {
     ['key1'] = 'value1';
     ['key1'] = 'value1';
  }
  }
and later alter the table to this:
and later alter the table in source to this:
  tableName = {
  tableName = {
     ['key1'] = 'value1';
     ['key1'] = 'value1';
     ['key2'] = 'value2';
     ['key2'] = 'value2';
  }
  }
tableName['key2'] will not be available for use unless you delete the [[SavedVariables]] file or put in extra code like this: (edit: [[User:Egingell|Egingell]] 09:11, 13 April 2007 (EDT)
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.
tableName = {}
 
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.
local tableNameDefaults = {
    ['key1'] = 'value1';
    ['key2'] = 'value2';
}
   
   
  function AddOnName_OnLoad()
  function AddOnName_OnLoad()
Line 117: Line 113:
  function AddOnName_OnEvent(event)
  function AddOnName_OnEvent(event)
     if event == 'VARIABLES_LOADED' then
     if event == 'VARIABLES_LOADED' then
         for k,v in pairs(tableNameDefaults) do
         if(not tableName.key1) then tableName.key1 = 'value1' end;
            if not tableName[k] then
        if(not tableName.key2) then tableName.key1 = 'value2' end;
                tableName[k] = v;
            end
        end
     end
     end
  end
  end
It might also work, if you declare an empty table first, like this:
 
tableName = {}
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.
tableName = {
 
    ['key1'] = 'value1';
    ['key2'] = 'value2';
}
[[User:Egingell|Egingell]] 09:04, 13 April 2007 (EDT)
[[Category: HOWTOs|Save Variables Between Game Sessions]]
[[Category: HOWTOs|Save Variables Between Game Sessions]]
Anonymous user