Motto:

"Clean programming is like art."


Lua‎ > ‎

Save table to file

Here is simple examle with class TStoreData, which alows save any user defined table structure to file and load it back as table.

Test script:

require ("class/TStoreData")

test_data =
{
    pos = {0,1,2},
    name = "player1",
    other =
                        {
                                items = {"item1","item2","item3"},
                                subitems = {"A","B"},
                                data = {
                                        x = 0,
                                        y = 1,
                                        z= 2},
                                tree = {
                                            leaf1 = "l1",
                                            leaf2 = "2",
                                            leaf3 = 
{
                                                      leaf31 = "31",
                                                      leaf32 = "32"},
                                                      used = true
                                                    }
                                      }
}

DataSaver = TStoreData:New()

-- save uncompressed data
DataSaver:Save("result.lua",false,test_data)

-- save compressed data
-- DataSaver:Save("result.lua",true,test_data)

Output:

return
{
        ["other"] = 
        {
                ["items"] = 
                {
                        [1] = "item1";
                        [2] = "item2";
                        [3] = "item3";
                };
                ["subitems"] = 
                {
                        [1] = "A";
                        [2] = "B";
                };
                ["data"] = 
                {
                        ["y"] = 1;
                        ["x"] = 0;
                        ["z"] = 2;
                };
                ["tree"] = 
                {
                        ["used"] = true;
                        ["leaf3"] = 
                        {
                                ["leaf32"] = "32";
                                ["leaf31"] = "31";
                        };
                        ["leaf1"] = "l1";
                        ["leaf2"] = "2";
                };
        };
        ["name"] = "player1";
        ["pos"] = 
        {
                [1] = 0;
                [2] = 1;
                [3] = 2;
        };
}


Example#1: Load Data

GameData = dofile("result.lua")

print(GameData.pos[1],GameData.pos[2],GameData.pos[3])
print(GameData.name)
print(GameData.other.items[1])
print(GameData.other.items[2])
print(GameData.other.items[3])

Example#2: Load Data

require ("class/TStoreData")

LoadData = TStoreData:New()
GameData = LoadData:Load("result.lua")

print(GameData.pos[1],GameData.pos[2],GameData.pos[3])
print(GameData.name)
print(GameData.other.items[1])
print(GameData.other.items[2])
print(GameData.other.items[3])

Note: Saved format looks little bit different, but this saved format is only other lua syntax. Work same like before. 
Č
ċ
ď
Ondrej Kollar,
Jun 22, 2010 12:26 PM