Game data storage?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
sunsflower
Prole
Posts: 18
Joined: Mon Jan 04, 2016 10:36 am

Game data storage?

Post 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? :awesome: thanks!
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Game data storage?

Post 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.
sunsflower
Prole
Posts: 18
Joined: Mon Jan 04, 2016 10:36 am

Re: Game data storage?

Post by sunsflower »

alright. binary 's not so good a idea after all......sûrement.
Thanks!
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Game data storage?

Post by Nixola »

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
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Game data storage?

Post 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? :awesome: 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.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Game data storage?

Post 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.
sunsflower
Prole
Posts: 18
Joined: Mon Jan 04, 2016 10:36 am

Re: Game data storage?

Post by sunsflower »

thanks a lot! :awesome: managed to make it with bitser.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Game data storage?

Post 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
Post Reply

Who is online

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