Thread Tools Display Modes
05-21-07, 12:23 PM   #1
Felankor
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: May 2007
Posts: 26
Question Problem Saving Arrays

Hi,

Sorry this is so long.

I read somewhere that if your saving a table and in a new version of your addon u add something to the table. If someone who had saved variables from your old version installed the new one, the new key u added to the table would not be saved. Understand? lol

I.e. If I had this in version 1:

Code:
Table = { key1 = "value1", key2 = "value2" }
and in version 2 I had:

Code:
Table = { key1 = "value1", key2 = "value2", key3 = "value3" }
if someone used version 1 and upgraded to version 2 without deleting their saved variables, the key3 wouldnt be saved each time they logged out. Or so I've read.

Where ever I read this also said to fix the problem make a table with the default keys and values and use the following:

Code:
for key, value in pairs(Table_Defaults) do 
        if (not Table[key]) then
          FF_Options[key] = value;
        end 
    end
First of all, is this true?

I have done what I said above, but my table is always replaced with the default so my variables never load the saved ones.

I have the following in my TOC file:

Code:
## SavedVariables: FF_Options
and then in the OnLoad event i have:

Code:
this:RegisterEvent("ADDON_LOADED");
and in the OnEvent I have:

Code:
if (event == "ADDON_LOADED") then
        if (arg1 == "AddOnName") then --My AddOn name is in the " "
            FF_LoadVariables();
        end
    end
I have the following in the FF_LoadVariables() function:

Code:
    for key, value in pairs(FF_Options_Defaults) do 
        if (not FF_Options[key]) then
          FF_Options[key] = value;
        end 
    end
Here is an example of my options tables (they are not in any function, just at the beginning of the lua file):

Code:
FF_Options_Defaults = {
    ['Option1'] = true,
    ['Option2'] = true,
    ['Option3'] = true,
    ['Option4'] = true,
    ['Option5'] = true
}

FF_Options = {
    ['Option1'] = true,
    ['Option2'] = true,
    ['Option3'] = true,
    ['Option4'] = true,
    ['Option5'] = true
}
If I use:

Code:
DEFAULT_CHAT_FRAME:AddMessage(table.getn(FF_Options))
in the FF_LoadVariables() function before it runs the for loop I get 0 printed to the default chat frame. This suggests that the table wasnt loaded. But if I go to the SavedVariables folder for my account and check the variables file for my addon the table is saved...

Also if I completely remove the loop the variables load to what ever they were saved as...

So what I want to know is:
Is what I read about the new key in the table not being saved true?
Why are the default variables always being loaded? OR Why dont any keys exist in FF_Options after the ADDON_LOADED event fires?
  Reply With Quote
05-21-07, 02:01 PM   #2
Tekkub
A Molten Giant
 
Tekkub's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 960
If you REPLACE the saved variable in your code, anything in it is lost. If you add/remove from it, but don't actually create a new table, then old settings stay, unless that setting is the thing you're modifying.
  Reply With Quote
05-23-07, 01:15 AM   #3
Felankor
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: May 2007
Posts: 26
Im not actualy replacing any settings.

Im checking if all the settings exist, and if they dont, im adding the new ones to the table.

I think I've fixed the problem now, just got to test it again to make sure.

The line "if (not FF_Options) then" doesn't like "false" values in the table.

So I've changed all my code to use 0 & 1 instead of true and false (0 being false & 1 being true)
  Reply With Quote
05-23-07, 01:20 AM   #4
Tekkub
A Molten Giant
 
Tekkub's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 960
you _should_ use true/false or true/nil. Check with "if somevar == nil" if you need to find nils and are saving falses. 0/1 is messy, you always have to test the value. Usually it's just simpler (and a ton cleaner) to use nil to define the default (then you don't have to load up defaults) and act against the default behavior if your var is set.

Rule of thumb: never save default values to savedvars, it's a waste of memory. I've seen addons that take 10MB to load a savedfar crammed full of only defaults. Very bad code that.
  Reply With Quote
05-23-07, 02:20 AM   #5
Shirik
Blasphemer!
Premium Member
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2007
Posts: 818
Originally Posted by Felankor
The line "if (not FF_Options) then" doesn't like "false" values in the table.

So I've changed all my code to use 0 & 1 instead of true and false (0 being false & 1 being true)
This is completely untrue and also an extremely bad idea. In Lua's mind, both 0 and 1 are true, and "if not FF_Options then" will never execute if FF_Options is either nil or false. Any other behavior indicates that what you are expecting FF_Options to be is not what it is.
__________________
たしかにひとつのじだいがおわるのお
ぼくはこのめでみたよ
だけどつぎがじぶんおばんだってことわ
しりたくなかったんだ
It's my turn next.

Shakespeare liked regexes too!
/(bb|[^b]{2})/
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Problem Saving Arrays


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off