Page 1 of 2
Loading/saving a table??
Posted: Tue Feb 16, 2016 2:18 pm
by rougan
Hiya, I have a pretty large table containing a couple of sub-tables (don't know what they're called
) that I want to write and read from a file so that I can save my game. Given some context, the table contains the properties for each block in the map, where there are 10 properties per block, 60 blocks per map and n maps. When I load the map, each block in the maps should be assigned the properties that I saved!
However, I've been struggling to find a way to properly read and write this table into just a text document.
Ser *sounds* like the best choice for me but I really don't understand what's going on in the readme and how I would use it:
Code: Select all
local serialize = require 'ser'
print(serialize({"Hello", world = true}))
-- prints:
-- return {"Hello", world = true}
My second idea was to write the table in lines of data seperated by commas and then use Lua's string functions to split the string at the commas, but I couldn't figure out how i'd use gmatch and match etc etc to split the string and it all got a bit messy!
If anyone has any other suggestions or could give me some help that'd be great, thanks very much!!
Re: Loading/saving a table??
Posted: Tue Feb 16, 2016 2:27 pm
by drunken_munki
I think that is serializing the data for you to a String that can be loaded directly back into Lua.
So you just need to save that string to a file, could you try:
Code: Select all
local serialize = require 'ser'
local serialdata = serialize({"Hello", world = true})
love.filesystem.write("test.lua", serialdata)
And check your user output directory for the "test.lua" file if that worked properly.
Re: Loading/saving a table??
Posted: Tue Feb 16, 2016 2:31 pm
by Robin
Hello! I'm the author of Ser (as well as Lady, Smallfolk and, as of recently, bitser).
First question: does it need to be a text file or is a binary file OK?
If it needs to be a text file, Ser is indeed a good idea. You'd use it like this:
Code: Select all
local serialize = require 'ser'
--when saving a game:
love.filesystem.write(filename, serialize(pretty_large_table))
--when loading a game:
pretty_large_table = love.filesystem.load(filename)()
If a binary file is OK, I recommend
bitser. You would use it like this:
Code: Select all
local bitser = require 'bitser'
--when saving a game:
love.filesystem.write(filename, bitser.dumps(pretty_large_table))
--when loading a game:
pretty_large_table = bitser.loads(love.filesystem.read(filename))
--or, faster but you need to keep a reference to "data"
local data = love.filesystem.newFileData(filename)
pretty_large_table = bitser.loadData(data:getPointer(), data:getSize())
Hope that helps!
Re: Loading/saving a table??
Posted: Tue Feb 16, 2016 3:12 pm
by drunken_munki
Robin wrote:Hello! I'm the author of Ser (as well as Lady, Smallfolk and, as of recently, bitser).
Well there you are, straight from the horse's mouth as it were.
Hi Robin, I'm using Ser in my game, I found it the most straight forward serialzer out there. Thank you for that.
Your new binary serialzer sounds intriguing, but alas I don't know much about this. Will a binary file be readable across different platforms?
Thanks.
Re: Loading/saving a table??
Posted: Tue Feb 16, 2016 3:37 pm
by bobbyjones
Yes. It should also produce smaller files in faster time.
Re: Loading/saving a table??
Posted: Tue Feb 16, 2016 8:05 pm
by rougan
Robin wrote:Hello! I'm the author of Ser (as well as Lady, Smallfolk and, as of recently, bitser).
First question: does it need to be a text file or is a binary file OK?
If it needs to be a text file, Ser is indeed a good idea. You'd use it like this:
Code: Select all
local serialize = require 'ser'
--when saving a game:
love.filesystem.write(filename, serialize(pretty_large_table))
--when loading a game:
pretty_large_table = love.filesystem.load(filename)()
If a binary file is OK, I recommend
bitser. You would use it like this:
Code: Select all
local bitser = require 'bitser'
--when saving a game:
love.filesystem.write(filename, bitser.dumps(pretty_large_table))
--when loading a game:
pretty_large_table = bitser.loads(love.filesystem.read(filename))
--or, faster but you need to keep a reference to "data"
local data = love.filesystem.newFileData(filename)
pretty_large_table = bitser.loadData(data:getPointer(), data:getSize())
Hope that helps!
Thanks very much Robin!! A text document would be fine so thank you for the helpful example, Ser definitely seems like the best solution now
Thank you for everyone's responses!
Re: Loading/saving a table??
Posted: Tue Feb 16, 2016 9:43 pm
by Robin
drunken_munki wrote:Hi Robin, I'm using Ser in my game, I found it the most straight forward serialzer out there. Thank you for that.
That's great to hear!
drunken_munki wrote:Your new binary serialzer sounds intriguing, but alas I don't know much about this. Will a binary file be readable across different platforms?
It should, although to be honest I haven't tested that yet.
Re: Loading/saving a table??
Posted: Mon Jul 22, 2024 6:58 am
by Lovingsoul1337
what if my table contains image data from love2d does this work then too ?
Re: Loading/saving a table??
Posted: Mon Jul 22, 2024 9:24 pm
by dusoft
Lovingsoul1337 wrote: ↑Mon Jul 22, 2024 6:58 am
what if my table contains image data from love2d does this work then too ?
You would either need binary serializer or encode your image data using well known algos such as base64 or similar.
Re: Loading/saving a table??
Posted: Thu Jul 25, 2024 7:23 am
by togFox
I try to embed a reference to those external things instead of putting them into the table.
Instead of a table of images I'll have a table of file path strings to those images. Can't always do that though.