Saving and Loading Tables (in text files?) [solved]
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Saving and Loading Tables (in text files?) [solved]
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.
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
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Saving and Loading Tables (in text files?)
This is untested but something like this may work
Also to have the same directory for the editor and game add this to conf.lua
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
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:
Re: Saving and Loading Tables (in text files?)
It's not tile based but I do something that looks like this:
when making objects in editor:
then it saves like this:
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.
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)
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
- 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?)
This is not going to work, The recursive part simply erases the string being buit...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
Re: Saving and Loading Tables (in text files?)
Easy enough to fix, just need to change it toRoland_Yonaba wrote:This is not going to work, The recursive part simply erases the string being buit...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
Code: Select all
if type(v)=="table" then
str = str.."{"..tableToString(v).."},\n"
else
Re: Saving and Loading Tables (in text files?)
Thats what i meant but as i said, it is untested.iemfi wrote: Easy enough to fix, just need to change it toalthough I don't see why you wouldn't just use a library.Code: Select all
if type(v)=="table" then str = str.."{"..tableToString(v).."},\n" else
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
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Saving and Loading Tables (in text files?)
No need to...Just a simple file containing a string to be executed as a chunk...mickeyjm wrote: Also i forgot to say i think that the file must be a .lua
I am wondering if love.filesystem.load is not actually just a binding to loadstring...
Re: Saving and Loading Tables (in text files?)
Well, you learn something new everyday!Roland_Yonaba wrote:No need to...Just a simple file containing a string to be executed as a chunk...mickeyjm wrote: Also i forgot to say i think that the file must be a .lua
I am wondering if love.filesystem.load is not actually just a binding to loadstring...
Your screen is very zoomed in...
- 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?)
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.
Who is online
Users browsing this forum: No registered users and 3 guests