Saving a lot of data

Hello. I’m making a game with a lot of data to save. Such as time, player stats, inventory, properities for a lot of separate game objects,…

I’ve watched some yt tutorials on save system, but they are always saving each variable by writing it separately in the save script, which is not possible here because of the very large amount.

I don’t really care about some encryption, because it’s single player only and cheat engine might probably do the work anyways. Also, want to save it on pc directory (not cloud) and the game is pc only.

How much is a “very large amount?”

You can write objects (ergo classes and structs) that themselves are serializable and simply write those to disk. Generally you’ll wrap up all your save data in one overarching class anyway.

Problem with most save tutorials is they’re writing the state of game objects/components in a scene to disk. Taking any scene state and recording that for saving is in general a tedious process.

Ideally if your game has a lot of persistent state, you store all of that separate from the scene to begin with, and the scene itself is a reflection of this state. If you’re smart and plan ahead, all this state is already good to save and you simply write your current state to disk.

Personally I’ve found you want to keep all your save state separate from your game assets as well; ergo nothing in your game’s assets should determine the state of a save game, all that should be the sole responsibility of your save data.

1 Like

I wouldn’t worry too much about the file size unless your paying for the cloud storage. It’s probably not going to be that large unless you’re doing something like recording replays. Large games like fallout are less than 30mb even in late games.

Probably the fastest way to save that uses the least amount of space is to manually write/read the binary data with BinaryWriter. However, I wouldn’t recommend this as it can be quite difficult to maintain when the data format changes (versions). You’ll also want to avoid binary serialization using BinaryFormatter as it can be a security issue e.g. someone could send you their save saying it has a bug and make you run a virus. Probably the safest and most common format is json. If you really need to reduce the filesize then a binary format like protobuf could be useful (handles versions) but I’ve never needed it.

One thing to watch out for with json is that the way you structure your data can affect the filesize e.g.the following is the same data.

Using a serialized class for items.
{“items”:[{“id”:0,“pos”:{“x”:0,“y”:0}},{“id”:1,“pos”:{“x”:1,“y”:0}},{“id”:2,“pos”:{“x”:2,“y”:0}},{“id”:3,“pos”:{“x”:3,“y”:0}},{“id”:4,“pos”:{“x”:4,“y”:0}},{“id”:5,“pos”:{“x”:5,“y”:0}},{“id”:6,“pos”:{“x”:6,“y”:0}},{“id”:7,“pos”:{“x”:7,“y”:0}},{“id”:8,“pos”:{“x”:8,“y”:0}},{“id”:9,“pos”:{“x”:9,“y”:0}}]}

Using lists for items
{“items”:{“id”:[0,1,2,3,4,5,6,7,8,9],“x”:[0,1,2,3,4,5,6,7,8,9],“y”:[0,0,0,0,0,0,0,0,0,0]}}

I’ve been tempted to look at using litedb recently. It’s a single file embedded NoSQL database (can store any type like json files). It also has ACID transactions and some basic encryption. It does make the filesize larger due to fragmentation but I like not having to worry about the data becoming corrupted due to a power cut.