Page 1 of 1

[SOLVED] Different approaches for saving settings and game progress

Posted: Wed Jul 18, 2018 10:56 am
by Link
I need to save game settings (e.g. video & audio settings) and game progress (e.g. which levels are completed, high-scores for each level).

What method are others using? Some ideas I had:
  • Using a Lua table structure to represent the settings, and serializing to a file (there seem to be many serialization libraries)
  • Saving to a JSON or XML file
  • Using an embedded database such as SQLite through a Lua wrapper library (would add complexity to the game distribution)
Thanks

Re: Different approaches for saving settings and game progress

Posted: Wed Jul 18, 2018 1:22 pm
by Nelvin
Use Lua ... there's hardly any sane reason not to do so for this kind of data.

Re: Different approaches for saving settings and game progress

Posted: Wed Jul 18, 2018 10:19 pm
by Nixola
One reason would be safety. Lua code would need to be sandboxed, or people sharing modified saves could potentially run untrusted code through them.

Re: Different approaches for saving settings and game progress

Posted: Thu Jul 19, 2018 1:29 am
by ivan
Yes, using Lua is by far the most sensible choice.
I like to load the default options (read-only) file as a table from the game directory,
then I look for a "custom" or saved options file in the appdata directory.
Then I merge the fields from the two tables if the types match:

Code: Select all

for k, v in pairs(default) do
  if type(v) == type(custom[k]) then
    default[k] = custom[k]
  end
end
This ensures that no junk gets loaded from the options file,
but you still need a little bit of sandboxing if you are going to use the appdata directory.
Always a good idea to separate the game settings from the progress save file.

Re: Different approaches for saving settings and game progress

Posted: Thu Jul 19, 2018 6:39 am
by Nelvin
Nixola wrote: Wed Jul 18, 2018 10:19 pm One reason would be safety. Lua code would need to be sandboxed, or people sharing modified saves could potentially run untrusted code through them.
Right, that's one of the few cases.

Controlling the environment (using setfenv) while loading a game might help in situations where additional safety is required.

Re: Different approaches for saving settings and game progress

Posted: Thu Jul 19, 2018 5:15 pm
by ivan
Actually, I would go one step further and say that regardless of the format, safety should always be of concern.
It doesn't really matter if you use Lua, XML, JSon, SQL - all of these formats could probably be exploited in one way or another.
It's really a question of what's practical in your case.

Re: Different approaches for saving settings and game progress

Posted: Thu Jul 19, 2018 7:29 pm
by Link
Thanks everyone, I'm going to stick with a Lua table using the techniques suggested. :cool:

Re: [SOLVED] Different approaches for saving settings and game progress

Posted: Fri Jul 20, 2018 5:58 am
by D0NM
You may use our module to save config into .lua file and load it later.
It has some defaults, etc.

https://github.com/thomasgoldstein/zabu ... ration.lua

Just add a reference to our site and the authors ))

Re: [SOLVED] Different approaches for saving settings and game progress

Posted: Fri Jul 20, 2018 10:21 am
by Link
D0NM wrote: Fri Jul 20, 2018 5:58 am You may use our module to save config into .lua file and load it later.
It has some defaults, etc.

https://github.com/thomasgoldstein/zabu ... ration.lua

Just add a reference to our site and the authors ))
Thanks, but your code seems to use the license "Attribution-NonCommercial 4.0 International" and I'm hoping to potentially sell my game (if it's ever finished). I'm sticking to libraries under the MIT License.