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

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.
User avatar
sanjiv
Citizen
Posts: 88
Joined: Mon Feb 27, 2012 5:11 am

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

Post 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.
Last edited by sanjiv on Sat May 26, 2012 10:58 pm, edited 1 time in total.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

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

Post by Roland_Yonaba »

Have you looked at TSerial?
I found thistoo on Lua-Users...
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

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

Post 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"
Your screen is very zoomed in...
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

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

Post 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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

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

Post 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...
iemfi
Citizen
Posts: 52
Joined: Fri Mar 30, 2012 11:03 am

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

Post 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.
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

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

Post 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
Your screen is very zoomed in...
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

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

Post 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...
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

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

Post 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!
Your screen is very zoomed in...
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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.
Post Reply

Who is online

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