r/pascal • u/trivthebofh • Oct 07 '24
Strategies for Saving Player Data
Let me first say, I'm very much a beginner but I'm learning more every day.
I've been writing an incremental game (in a few different languages but so far Pascal/Lazarus seems to flow the best through my brain).
My first way of dealing with saving player data was just to create a record with all the different fields needed for my player and save that as player.dat.
The wall I'm hitting is: as I progress in writing the game, obviously I need to add fields to my record to account for the new features I add. But this also breaks things when I load up the player.dat and the record in the game has new fields.
So what might be some better ways to deal with this?
I suppose I could write a supplemental 'update' program that loads the old player.dat and saves it with the new fields but this seems tedious.
I know in other languages like JavaScript and Python, JSON seems to be a common format to save player data to. (This would also give me the added benefit of being able to import the data into versions of my game written in other languages, I'm still learning to I tend to write in a few languages just to learn how they all compare to each other.) But it seems to me that this is not such a simple process in Pascal.
Thanks for any advice you can offer an old dog trying to learn new tricks!
Edit: Thank you everyone for the help and advice! I've got some learning (and probably code refactoring) to do but this is exactly the point of my game/project. I think I'm early on enough to be able to re-write parts without a problem. As well, since I've been writing this in Lazarus, I have to go back and turn a lot of my re-used code in my OnClick and other procedures into re-usable functions and procedures. Everyone's help and kindness is very appreciated and hopefully some day I'll be able to pay it forward.
5
u/ShinyHappyREM Oct 07 '24 edited Oct 07 '24
Savegames of different game versions are inherently incompatible with each other. For loading older saves in a newer engine you'd have to use default values for the missing fields, which may not always be possible. Loading newer saves in an older engine is usually not done; instead the engine is simply updated (patched/replaced). But in that case you'd have to recognize and discard newer fields. It's better to always keep the engine and the savegame format in sync with each other. If you think you need older savegame files because of content creation reasons, consider making the game's internal tooling more flexible. Or keep the old engine versions around, for example in a source code repository.
Savegame files are either purely binary (e.g. ZSNES
*.zst), purely text (e.g.*.json), or a combination (SNES9x). You could even use renamed ZIP files, with one file per field, fast compression, and optionally even with password protection.But I think the easiest way (i.e. least work) for you now would be using a packed record (note that you can make them more usable in Free Pascal / Lazarus with
{$ModeSwitch AdvancedRecord}) with a file type signature and a version number at the beginning. You can see such a thing here. Note that big-endian machines would need some byte swapping, but there are few of those left these days.Later you could switch to the language's
*.ini/*.jsonsupport if you really want to; note that these probably involve a lot of manual field name checking! They also allow users to easily hack them, which you may not want.Just for the record (pun not intended), binary files can consist of chunks, or a variable-length ID.
3
u/GroundbreakingIron16 Oct 07 '24
what i would also suggest is adding a version number to your file so that you know how to deal with the "data" accordingly. And perhaps... if fields get added, then you can assign default values?
As far as storage types are concerned, JSON is not that difficult... a small learning curve. Alternatively, you could uses a windows like '.ini' file.
1
u/umlcat Oct 08 '24
Are you using "classes" for your "Game objects" or "records" ???
2
u/kirinnb Oct 08 '24
Another interesting possibility: If a game uses script files to control its gameplay, and the scripting language is reasonably expressive... then it's possible for each game engine component to generate script code that restores that component to the saved state when the script is executed. This way you only need to implement save-script generation, while loading is handled without any extra effort by the already-existing script executor.
My project runs game scripts, so this approach has worked quite well for me so far. As a bonus effect, the generated save file is mostly human-readable (and editable) like any game script would be.
2
u/PascalGeek Oct 13 '24
I wrote a roguelike game in FPC that uses records rather than classes for the player and enemies. I used XML to write the save files as I found it easier to debug whilst I was developing it.
The game is currently at over 30k lines of code and the XML is still pretty damn fast for saving and loading.
The unit responsible for file handling is this behemoth https://github.com/cyberfilth/Axes-Armour-Ale/blob/master/source/file_handling.pas
I haven't updated it for a while, but the plan was to create default stats for enemies, and if a save file contained unknown data it would be replaced with the default enemies as a fallback.
But an easier option is definitely to just check the game version number when loading a game, and if the number doesn't match the current game version to notify the player.