I’m making a game in Phaser 3 and wondering about best practices for storing dynamic data about tiles, collected items, settings, etc.
The game I’m working on is about farming, collecting resources, build things, build relationships with villagers etc. I want to be able to store all collected resources and the progress of my growing plants so that, when I reload the browser tab (or close it and enter localhost:8000 again), everything is still the same as last time I played the game.
What tools/frameworks would you use to solve a similar task?
One thing to keep in mind for releasing the game is that players who use private browsing or who clear their browser data will lose their progress. This is the browser working as intended, but in the past I’ve had many players complain about it regardless. I like to offer an option that will download a simple JSON file of their save data that they can upload later if they want the piece of mind of being able to keep their data.
The easiest solution that you can find to implement.
But only one thing to keep in mind, some users are actually blocking the usage of localStorage so it’s best to check if it’s actually defined on window first
if('localStorage' in window) {
// then maybe use or get a referance to it
}
else {
// or else find some other solution?
}
Note, too, that localStorage APIs can throw an error if the user chooses to disable them. localForage is a useful library if you want to avoid handling such edge cases yourself.
Yeah losing the progress after clearing the browser data is something I don’t want to happen. I guess I can’t use localStorage to write the data to a JSON file since localStorage is read only? What libs/tools did you use to save the data to a JSON file?
I used JSZip: https://stuk.github.io/jszip/
(Note: This was a good five years ago now, so I can’t be sure how well it works these days.)
I would recommend making a Storage class that handles all of your saving/reading saved game data for you that wraps up whatever your implementation is. That way everywhere in your code you can use MyStorage.Save(data); or whatever and then if you change libraries/methods you only have to change it in that one place. You can use something simple like localStorage for your own playtesting, then you can swap out the implementation of MyStorage for something else once you plan to release it to other people.