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.
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.
Lovingsoul1337
Citizen
Posts: 61
Joined: Fri Feb 21, 2020 1:26 pm

Re: Loading/saving a table??

Post by Lovingsoul1337 »

what if my table contains image data from love2d does this work then too ?
User avatar
dusoft
Party member
Posts: 655
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Loading/saving a table??

Post 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.
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Loading/saving a table??

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests