Page 1 of 2

Saving and Loading Tables (in text files?) [solved]

Posted: Thu Apr 26, 2012 3:37 am
by sanjiv
I simply need to store some tables in one instance of a game, and then load them in another.

context:
Platformer.
Different objects are stored in different tables based on their object type. (I.e. one table for platforms, another for enemies)
I want to create level editor, where I instantiate different objects onto a grid area (i.e. create platforms, and place enemy positions)

need:
To save the results of the level editor.
I think this means that the various tables of objects need to be stored to a file.

Re: Saving and Loading Tables (in text files?)

Posted: Thu Apr 26, 2012 11:23 am
by Roland_Yonaba
Have you looked at TSerial?
I found thistoo on Lua-Users...

Re: Saving and Loading Tables (in text files?)

Posted: Thu Apr 26, 2012 5:05 pm
by mickeyjm
This is untested but something like this may work

Code: Select all

local FILE = "YourSaveDirectory"

function tableToString(t) -- In case of multi dimensional arrays
local str = ""
    for k,v in pairs(t) do
        if type(v)=="table" then
            str = "{"..tableToString(v).."},\n"
        else
            str = str..k.."="..v..","
        end
    end
return str
end

function save(table)
if love.filesystem.exists(FILE) then
    love.filesystem.remove(FILE)
end
love.filesystem.newFile(FILE)
success = love.filesystem.write(FILE,tableToString(table))
end

function load()
if love.filesystem.exists(FILE) then
    love.filesystem.load(FILE)()--"Runs" the file i.e loads the table
end
end
Also to have the same directory for the editor and game add this to conf.lua

Code: Select all

function love.conf(t)
--Rest of conf file here
t.indentity = "YourDirectory"

Re: Saving and Loading Tables (in text files?)

Posted: Thu Apr 26, 2012 6:05 pm
by The Burrito
It's not tile based but I do something that looks like this:

when making objects in editor:

Code: Select all

makeThing(x,y,a,def)
local t = {}
t.x = x
t.y = y
t.a = a
t.d = def
(do the stuff that actually makes the object)
table.insert(things,t)
then it saves like this:

Code: Select all

for i,v in ipairs(things) do
love.filesystem.write( filename, "makeThing("..v.x..","..v.y..","..v.a..","..v.d..")\n")
end
This is from memory so it's probably wrong (don't have access to my code right now). I prefer to generate readable files so they're fairly easy to edit by hand if need be. To load rather than parsing the file back into a table it runs like any other lua file. This means I can use this as a way to inject custom code into specific maps and stuff like that.

Re: Saving and Loading Tables (in text files?)

Posted: Fri Apr 27, 2012 9:06 am
by Roland_Yonaba
mickeyjm wrote:This is untested but something like this may work

Code: Select all

local FILE = "YourSaveDirectory"

function tableToString(t) -- In case of multi dimensional arrays
local str = ""
    for k,v in pairs(t) do
        if type(v)=="table" then
            str = "{"..tableToString(v).."},\n"
        else
            str = str..k.."="..v..","
        end
    end
return str
end


This is not going to work, The recursive part simply erases the string being buit...

Re: Saving and Loading Tables (in text files?)

Posted: Fri Apr 27, 2012 1:12 pm
by iemfi
Roland_Yonaba wrote:
mickeyjm wrote:This is untested but something like this may work

Code: Select all

local FILE = "YourSaveDirectory"

function tableToString(t) -- In case of multi dimensional arrays
local str = ""
    for k,v in pairs(t) do
        if type(v)=="table" then
            str = "{"..tableToString(v).."},\n"
        else
            str = str..k.."="..v..","
        end
    end
return str
end


This is not going to work, The recursive part simply erases the string being buit...
Easy enough to fix, just need to change it to

Code: Select all

if type(v)=="table" then
            str = str.."{"..tableToString(v).."},\n"
        else
although I don't see why you wouldn't just use a library.

Re: Saving and Loading Tables (in text files?)

Posted: Fri Apr 27, 2012 2:58 pm
by mickeyjm
iemfi wrote: Easy enough to fix, just need to change it to

Code: Select all

if type(v)=="table" then
            str = str.."{"..tableToString(v).."},\n"
        else
although I don't see why you wouldn't just use a library.
Thats what i meant but as i said, it is untested.
Also i forgot to say i think that the file must be a .lua

Re: Saving and Loading Tables (in text files?)

Posted: Fri Apr 27, 2012 4:33 pm
by Roland_Yonaba
mickeyjm wrote: Also i forgot to say i think that the file must be a .lua
No need to...Just a simple file containing a string to be executed as a chunk...
I am wondering if love.filesystem.load is not actually just a binding to loadstring...

Re: Saving and Loading Tables (in text files?)

Posted: Fri Apr 27, 2012 6:20 pm
by mickeyjm
Roland_Yonaba wrote:
mickeyjm wrote: Also i forgot to say i think that the file must be a .lua
No need to...Just a simple file containing a string to be executed as a chunk...
I am wondering if love.filesystem.load is not actually just a binding to loadstring...
Well, you learn something new everyday!

Re: Saving and Loading Tables (in text files?)

Posted: Fri Apr 27, 2012 6:55 pm
by bartbes
love.filesystem.load opens the file, reads it contents and loads that, if that's what you mean. It's the love.filesystem alternative to loadfile.