How to save entire contents of a table to a text file?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How to save entire contents of a table to a text file?
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.
EDIT: Oh and did I mention it freezes at startup if it's made song.txt in the appdata folder.Re: How to save entire contents of a table to a text file?
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
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 ...
Re: How to save entire contents of a table to a text file?
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 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
Re: How to save entire contents of a table to a text file?
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: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.
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 ,
}
]]
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 ...
Re: How to save entire contents of a table to a text file?
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?spir wrote: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: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.
(Note the disorder .)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 , } ]]
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
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
Re: How to save entire contents of a table to a text file?
you can do it like this:
(“{ a = 1, c = 3, b = 2, }” should turn into “{ a = 1, c = 3, b = 2 }”)
Code: Select all
string = string.gsub (string,", }"," }")
Re: How to save entire contents of a table to a text file?
Thanks! It still freezes on startup if there's a song.txt in the appdata folder though, so we're not quite done yet.Wojak wrote:you can do it like this:(“{ a = 1, c = 3, b = 2, }” should turn into “{ a = 1, c = 3, b = 2 }”)Code: Select all
string = string.gsub (string,", }"," }")
Re: How to save entire contents of a table to a text file?
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" )
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" )
Re: How to save entire contents of a table to a text file?
It's for when I implement a separate level editor. I can comment that part out for now.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" )
I noticed on the Wiki that streaming means that it uses less memory, but I only want it to play once.
Re: How to save entire contents of a table to a text file?
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
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests