Loading/saving a table??

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.
Post Reply
User avatar
rougan
Citizen
Posts: 58
Joined: Wed Aug 12, 2015 10:30 am

Loading/saving a table??

Post by rougan »

Hiya, I have a pretty large table containing a couple of sub-tables (don't know what they're called :P ) 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!! :nyu:
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: Loading/saving a table??

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Loading/saving a table??

Post 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!
Help us help you: attach a .love.
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: Loading/saving a table??

Post 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.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Loading/saving a table??

Post by bobbyjones »

Yes. It should also produce smaller files in faster time.
User avatar
rougan
Citizen
Posts: 58
Joined: Wed Aug 12, 2015 10:30 am

Re: Loading/saving a table??

Post 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!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Loading/saving a table??

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 50 guests