Page 1 of 1
Game data storage?
Posted: Sat Nov 05, 2016 1:37 am
by sunsflower
I'm making a small game when I come across this problem...If I want to serialize some game objects to string and save it in a plaint text file, the player may be able to modify it. But lua doesn't seem to have any functionalities for directly manipulate binary file(write bytes, I mean). So what's the common solution for game serialization?
thanks!
Re: Game data storage?
Posted: Sat Nov 05, 2016 2:12 am
by Jasoco
You can just output save files as a table. Forget binary. If it's a small game you don't really need it to be binary. Plenty of games are fine with plain text save files.
I use a library called
DataDumper.lua. It takes a table and turns it into plain text which you can write to the save file. It only saves tables that dont have any weird data in them like functions. But your save file data probably won't have functions anyway. As long as you structure the game's data properly you can make it easy to save a whole table.
Then you load the save file back in using the table = love.filesystem.load(savefile)() method where you either use it straight or you run through it and set things based on their values.
You can use the same method for preferences and options.
Re: Game data storage?
Posted: Sat Nov 05, 2016 2:35 am
by sunsflower
alright. binary 's not so good a idea after all......sûrement.
Thanks!
Re: Game data storage?
Posted: Sat Nov 05, 2016 10:08 am
by Nixola
I would use something like bitser instead; outputting Lua code means people can distribute malicious save files.
Re: Game data storage?
Posted: Sat Nov 05, 2016 12:48 pm
by ivan
sunsflower wrote:lua doesn't seem to have any functionalities for directly manipulate binary file(write bytes, I mean)
It's certainly possible to write to files in binary mode using io.open(filename, 'wb') - love2d has an equivalent function for this. Since Lua is an interpreted language with its own bytecode - it doesn't make sense to use binary at all.
So what's the common solution for game serialization?
thanks!
The easiest way to load code/data is using "loadstring". If you write your Lua table/code to a text file it's trivial to load it back again (there are libs for this, for example Ser).
Personally, I would advise against serializing objects - your game could change in the future, so it's probably better to save pure data (tables only without code). Basically, you want to have a save format that remains the same even when your code changes.
Re: Game data storage?
Posted: Sat Nov 05, 2016 2:34 pm
by pgimeno
My personal favorites are
Smallfolk (for text-based serialization) and
bitser (for a more compact, binary-only representation). Neither of them loads the data as a Lua program, making them secure.
Re: Game data storage?
Posted: Sun Nov 06, 2016 7:08 am
by sunsflower
thanks a lot!
managed to make it with bitser.
Re: Game data storage?
Posted: Sun Nov 06, 2016 8:15 am
by ivan
pgimeno wrote:Neither of them loads the data as a Lua program, making them secure.
It possible to load Lua strings using "setfenv" which disables access to any globals:
Code: Select all
function ploadstring(s)
local f, e = loadstring(s)
if not f then
error(e) -- syntax error
end
setfenv(f, {})
local ok, e = pcall(f)
if not ok then
error(e) -- runtime error
end
return e
end