Jump to content

Save data to lua syntax (as text/compressed)


AndyGFX
 Share

Recommended Posts

File Name:Save data to lua syntax (as text/compressed)

File Submitter: AndyGFX

File Submitted: 01 Jun 2010

File Updated: 01 Jun 2010

File Category: Lua Scripts

 

I wrote TStoreData.lua class for simple save data to standard lua format, which is simple to read back to script (described here: HowTo using LUA script as data file)

 

Example: Save Data

 

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.

 

Click here to download this file

  • Upvote 2

[HW] C2D Q6600, 4GB RAM, NV8800GTX, Vista Ultimate x64

[sW] Blide Plus, BlitzMax, Delphi, C++, 3DWS 5.53, Leadwerks 2.xx

 

76561197970156808.pngAndyGFX.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...