I’ve been developing a Terraria-based game for a while now as a hobby, and I’m just getting back into world saving and loading (I tried to create system for it before, but failed miserably). Anyway, I’ve written up a specification for how the data should be stored, and I wanted to get some feedback before I actually write the code for it. Hopefully it’s understandable, and not too boring. Thanks!
I have no idea how binary files work, but I can say a few things.
Unless you know 100% what you’re doing and why you are doing it, I wouldn’t advice you to take the route of binary save file… A simple json or xml should be plenty enough for 2d games.
Since you’re asking for feedback, I would assume that you don’t know what you are doing
I have an idea of what I’m doing; I just wanted to see if I could be doing anything better before implementing my plan. I’ve chosen to take the path of binary because a world in the game could contain potentially millions of blocks, and if I were to store their data in plain text, each save file would occupy a relatively huge amount of space on the hard drive (probably several dozen to several hundred megabytes). Whereas, with binary, the file can be compressed down to potentially a few megabytes.
Assuming your world is procedurally generated:
If i were you i would,
Simply save the string you used to generate the map (avoid using Math.random or etc.), then simply log changes to the map by specifying which chunk has been changed and how. you could also probably cache the players immediate location for quick start up time. Everything else looks decent imo from skimming it, and documenting it well as you have is a big plus.
This is also assuming that I understood what you were doing correctly. If i did it seemed like you were going to save the entire map to the file.
I wouldn’t worry about blocks. Blocks are pretty easy to do.
I would worry about how you handle things like trees and stuff (Unless trees are already blocks)
If I were you, and my game didn’t need to save entities (trees, bushes and stuff like that, that would go into world generation), I would do something like this:
- Every time player opens their world, randomly generate the part of the world the player is currently in;
- Read a world save file, which should contain tiles that the player has changed;
- Apply these tiles to your randomly generated world;
- Boom. Now you’re saving only the tiles that the player has modified from the original map seed;
I’m not so sure I want to use that approach. That would prohibit me from modifying the terrain generator later on, which I don’t want to do.
How does that work? You only generate game world when you create ‘generate’ in the game menu. When you ‘generate’ your world, you would probably only generate a seed for your world.
I bet minecraft uses the same approach, but instead of saving what tiles have been changed, it probably saves what chunks have been visited by the player.
You are correct to an extent.
But let me ask this, do you generate the whole map when you make a new game or do you just keep generating as it goes on. If you keep generating and you change the terrain generator it will lead to inconsistency,
You could have a little piece in the file called the terrain generation algorithm version used, and generate according to that.
If you want to deal with the inconsistancy I mentioned then you might as well just have the old terrain algorithm generate the map again, save the whole map you have so far, then change the algorithm for the rest of the map.
If you load the entire map, then once you make an update to terrain generator, you could make the current client save the entire map as you are doing now and have new maps run the new terrain generator with only saving changes as I mentioned before. In this case, older map files will be a lot larger, but newer ones will be a lot slimmer.
Now, if your files are small and you generate the whole thing in one go, you might as well save the whole map as you have been doing. The sacrifice in memory is probably worth the simplicity.