How to save entire contents of a table to a text file?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Mario-Fan
Prole
Posts: 18
Joined: Wed Jun 06, 2012 6:37 pm

How to save entire contents of a table to a text file?

Post by Mario-Fan »

I'm attempting to make a game that's a bit like Guitar Hero. It's currently at a very basic stage with no real eye candy. For the level editor I'm going to be using, I'm going to want to save the entire contents of four tables to a text file. Thing is, with the methods I've tried, it only saves the last item in the table. I haven't coded the editor yet, but I have coded it to save the file format of the music used (so far it only recognises WAV, MP3 and OGG because I don't really know of any others, which is another question I'd like answered since the wiki doesn't list all of them) in the same file, and using testing values, I have noticed that I couldn't find it in the huge database that is my knowledge and would like some help. (To think that I thought I'd be able to make this without asking you guys for help. Note to self, never think that thought again -_-) Here's the code. I put in Gangam Style so that I could test the level editor and gameplay when I put them in.
asdfhero.love
So incomplete it doesn't even have gameplay, only menus :P
(2.82 MiB) Downloaded 164 times
EDIT: Oh and did I mention it freezes at startup if it's made song.txt in the appdata folder.
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: How to save entire contents of a table to a text file?

Post by spir »

You'll find a complete discussion of the topic (I mean your thread title) and its problems in the online Lua manual (it's for lua 2.0, but still mostly up-to-date).

manual: http://www.lua.org/pil/#online

serialisation: http://www.lua.org/pil/12.html

A few questions:

1. Are the keys all ids (strings)?

2. What are the values?

3. Are there table values? If yes, you'll need a recursive func to serialise sub-tables.

4. If yes, are there possible shared tables? If yes, to avoid multiple serialisation, you need a way to identify shared (sub-)tables, namely their names.

5. If yes, are there possible cycles? If yes, same thing.

You can easily write a table serialisation func covering sub-tables (point 3.) and keeping a list of already-seen tables (references, in fact), then when meeting one write an erroneous placeholder such as "<shared>" which will cause an error on reloading and tell you about the issue.
More help if needed.

I don't understand much about the rest of your post...

Denis
... la vita e estrany ...
Mario-Fan
Prole
Posts: 18
Joined: Wed Jun 06, 2012 6:37 pm

Re: How to save entire contents of a table to a text file?

Post by Mario-Fan »

spir wrote:You'll find a complete discussion of the topic (I mean your thread title) and its problems in the online Lua manual (it's for lua 2.0, but still mostly up-to-date).

manual: http://www.lua.org/pil/#online

serialisation: http://www.lua.org/pil/12.html

A few questions:

1. Are the keys all ids (strings)?

2. What are the values?

3. Are there table values? If yes, you'll need a recursive func to serialise sub-tables.

4. If yes, are there possible shared tables? If yes, to avoid multiple serialisation, you need a way to identify shared (sub-)tables, namely their names.

5. If yes, are there possible cycles? If yes, same thing.

You can easily write a table serialisation func covering sub-tables (point 3.) and keeping a list of already-seen tables (references, in fact), then when meeting one write an erroneous placeholder such as "<shared>" which will cause an error on reloading and tell you about the issue.
More help if needed.

I don't understand much about the rest of your post...

Denis
Erm, I want the program to put numbers into the tables in the text file and then be able to load them. I want it to save the times in the song when you have to press a button, if that makes any sense. I tend not to think about programming terms since I don't discuss programming all that much because most of the people from the forum I come from that have any good experience in programming use other languages, so I don't really understand most of your post. What I do understand is that in this game, I'm not planning on having tables in tables and that I want number values in the tables, not strings.
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: How to save entire contents of a table to a text file?

Post by spir »

Mario-Fan wrote: Erm, I want the program to put numbers into the tables in the text file and then be able to load them. I want it to save the times in the song when you have to press a button, if that makes any sense. I tend not to think about programming terms since I don't discuss programming all that much because most of the people from the forum I come from that have any good experience in programming use other languages, so I don't really understand most of your post. What I do understand is that in this game, I'm not planning on having tables in tables and that I want number values in the tables, not strings.
Then, it should be far easier. If you want to edit the file content by program, then maybe do not indent the text. Something like:

Code: Select all

write_table = function (t)
   print ("{")
   for k,v in pairs(t) do
      print(k .. " = " .. v .. " ,")
   end
   print "}"
end

t = {a=1 , b=2 , c=3}
write_table(t) -->
--[[
{
a = 1 ,
c = 3 ,
b = 2 ,
}
]]
(Note the disorder :huh: .)
To edit, read the file's lines into a table (function file.lines, removes newlines) and do your string processing. Then re-concat (with newline) and rewrite into the file.

Denis
... la vita e estrany ...
Mario-Fan
Prole
Posts: 18
Joined: Wed Jun 06, 2012 6:37 pm

Re: How to save entire contents of a table to a text file?

Post by Mario-Fan »

spir wrote:
Mario-Fan wrote: Erm, I want the program to put numbers into the tables in the text file and then be able to load them. I want it to save the times in the song when you have to press a button, if that makes any sense. I tend not to think about programming terms since I don't discuss programming all that much because most of the people from the forum I come from that have any good experience in programming use other languages, so I don't really understand most of your post. What I do understand is that in this game, I'm not planning on having tables in tables and that I want number values in the tables, not strings.
Then, it should be far easier. If you want to edit the file content by program, then maybe do not indent the text. Something like:

Code: Select all

write_table = function (t)
   print ("{")
   for k,v in pairs(t) do
      print(k .. " = " .. v .. " ,")
   end
   print "}"
end

t = {a=1 , b=2 , c=3}
write_table(t) -->
--[[
{
a = 1 ,
c = 3 ,
b = 2 ,
}
]]
(Note the disorder :huh: .)
To edit, read the file's lines into a table (function file.lines, removes newlines) and do your string processing. Then re-concat (with newline) and rewrite into the file.

Denis
Nice! I've edited it so that it turns it into a string (and then prints it to console so that I can see how it looks instantly), but I can't work out how to get rid of the comma on the last entry (it's necessary to do that with tables, I've noticed before that the program crashes if there is a comma on the last entry). v seems to think it's always more than #t, so trying to compare them doesn't work. What next?

Code: Select all

write_table = function (t)
	local string = "{ "
	for k,v in pairs(t) do
		if v < #t then -- it never thinks this is true
			string = string .. k .. " = " .. v .. " "
		elseif v > #t then -- it always thinks this is true
			string = string .. k .. " = " .. v .. ", "
		end
	end
	string = string .. "}"
	print(string) -- When the table's {a=1, b=2, c=3}, it produces { a = 1, c = 3, b = 2, } I don't understand the order, but when a, b and c are numbers it's in the normal order
end
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: How to save entire contents of a table to a text file?

Post by Wojak »

you can do it like this:

Code: Select all

 string = string.gsub (string,", }"," }") 
(“{ a = 1, c = 3, b = 2, }” should turn into “{ a = 1, c = 3, b = 2 }”)
Mario-Fan
Prole
Posts: 18
Joined: Wed Jun 06, 2012 6:37 pm

Re: How to save entire contents of a table to a text file?

Post by Mario-Fan »

Wojak wrote:you can do it like this:

Code: Select all

 string = string.gsub (string,", }"," }") 
(“{ a = 1, c = 3, b = 2, }” should turn into “{ a = 1, c = 3, b = 2 }”)
Thanks! It still freezes on startup if there's a song.txt in the appdata folder though, so we're not quite done yet.
asdfhero.love
Still no gameplay :P
(2.82 MiB) Downloaded 292 times
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: How to save entire contents of a table to a text file?

Post by Wojak »

the text file is ok, the sound file is the problem...

You are using love.filesystem.exists on a sound file, while the file will exist only inside the love file...
You are using a “static” mode for a file 3MB large (I recommend "stream" )
Mario-Fan
Prole
Posts: 18
Joined: Wed Jun 06, 2012 6:37 pm

Re: How to save entire contents of a table to a text file?

Post by Mario-Fan »

Wojak wrote:the text file is ok, the sound file is the problem...

You are using love.filesystem.exists on a sound file, while the file will exist only inside the love file...
You are using a “static” mode for a file 3MB large (I recommend "stream" )
It's for when I implement a separate level editor. I can comment that part out for now.
I noticed on the Wiki that streaming means that it uses less memory, but I only want it to play once.
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: How to save entire contents of a table to a text file?

Post by Wojak »

this is working on my PC:

Code: Select all

if songFile and love.filesystem.exists(love.filesystem.getSaveDirectory( ).."/"..songFile) then
	    songMusic = love.audio.newSource(songFile, "stream")
	end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests