Game data storage?
-
- Prole
- Posts: 18
- Joined: Mon Jan 04, 2016 10:36 am
Game data storage?
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!
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Game data storage?
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.
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.
-
- Prole
- Posts: 18
- Joined: Mon Jan 04, 2016 10:36 am
Re: Game data storage?
alright. binary 's not so good a idea after all......sûrement.
Thanks!
Thanks!
Re: Game data storage?
I would use something like bitser instead; outputting Lua code means people can distribute malicious save files.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: Game data storage?
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.sunsflower wrote:lua doesn't seem to have any functionalities for directly manipulate binary file(write bytes, I mean)
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).So what's the common solution for game serialization? thanks!
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?
-
- Prole
- Posts: 18
- Joined: Mon Jan 04, 2016 10:36 am
Re: Game data storage?
thanks a lot! managed to make it with bitser.
Re: Game data storage?
It possible to load Lua strings using "setfenv" which disables access to any globals:pgimeno wrote:Neither of them loads the data as a Lua program, making them secure.
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
Who is online
Users browsing this forum: No registered users and 4 guests