Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
sanjiv
Citizen
Posts: 85
Joined: Mon Feb 27, 2012 5:11 am

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.

Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

Have you looked at TSerial?
I found thistoo on Lua-Users...

mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

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

if love.filesystem.exists(FILE) then
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...

The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

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.

Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

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

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.

mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

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

Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

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

mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

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

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact: