how to go to be considered when making save/load functionality?

Started by
11 comments, last by frob 10 years ago

Hi,

I done some googling and reading but am still unsure how I should make my save "file", I was thinking about making a text file for readability, but what should I "save" into the file so I don't end up with a thousand differently save files.

Me and my friend is trying to make a RPG-ish game (2D), so we got stuffs like health and inventory etc. How am I suppose to save my "inventory" into the file? for health I thougth about just saving it in plain number format but what am I suppose to do with the inventory? should I check each slots to see what item it is and save the said item into a a "Inventory" folder with seperated files as "Slot 01".txt with infomation about the item? I was thinking about just saving itemID but we decided to try to have "Randomized" items where they have a chance to have different component that grant stuffs(ex. Double jump).

Advertisement

You can save a json string (or something similar) into one text file, or an XML file. Those formats let you save a key and a value for everything you want to save and you can nest key-value pairs as a value of other key for things like storing array/map data. Some samples here: http://json.org/example.html

If you choose those formats you'll probably find also a library to read and write that kind of files, without implementing the parsers yourself.

If the items can have random values you can store the values in those formats and keep it readable, but you might want to encrypt the information in the future so the players can't just edit the save file to get things (which would make the "readable" requirement impossible).

Also, if you don't use those or other standard formats, the idea of different files for different items sounds really bad. Everything will be organized, but you'll have to add a lot more logic (references from one file to another, file creation and deletion for new or removed items, you'll have to check if an item isn't already saved in one file if you save again, etc).

Unless you really have a need to reference different files, like in Minecraft for example (where things are very dynamic), you should keep the save to one data file.

You should use a binary file, so that it is 1: Harder for most people to cheat, and 2: it is much faster to write and read a save with binary. Who needs readability for a format like this? If you want to see what it contains, write a simple Save Viewer or something.

Since a computer doesn't need to look at a text saying "slot 0", "slot 1", "health", etc..., you can just skip that part.

You can have a byte which has the slot count, and then a byte or two with health.

Something else you should really pay attention to is versioning the formats. If you notice a big design flaw in the save format after game release, you want to let the player be able to convert their old save file to the new format. I'm sure the players would be upset if an update just deleted their save.

If you want to read the save file without a program, then XML and JSON are good alternatives.


Since a computer doesn't need to look at a text saying "slot 0", "slot 1", "health", etc..., you can just skip that part.
You can have a byte which has the slot count, and then a byte or two with health.

Something else you should really pay attention to is versioning the formats.

When working with binary files, you also need to pay attention to endianness.

Well, there different aproaches to this kind of problem.

Usually a save is loaded into memory, extracting the values you want to use, by doing this you can modify those stored values, assemble the save file and write to disk.

if you want readabilty, you can make a readable file save using a format you desing or JSON, for example

JSON


inventory: {
	slot1: [itemID1, itemID2, itemID3, itemID4],
	slot2: [{potionbagID1: [potionID1, potionID2, potionID3]}, itemID2]
}

by doing so you just need to parse each value, if you want to save you write the file with updated values.

CUSTOM FORMAT


[Inventory]

slot1: itemID1; itemID2; itemID3; itemID4;
slot2; bagitemID; itemID2;
bagitemID: potionID1; potionID2; potionID3;

[equipment]

slot1: itemID1; itemID2;

Same as JSON, but you just have to code your own parsing rules for a format like this.

There's one more type of file save most comonly used because it's efficient and is file-size friendly

BINARY FORMAT


010203010201

By having this your program knows internally that at offset 0x00 up to offset 0x02 there will be the space for saving items in slot1, up to 3 items can be saved until you somehow tell your program that items can be allocated dynamically, from 0x02 up to 0x03 there will be the allocation space for slot2 up to 2 items can be saved, and so on.

I highly recommend the binary format since it's easier to parse, using custom formats will allow the player modify them with more ease, while binary formats can be encrypted or obfuscated and confuse users.

If you're using a C++11 compliant compiler you can check out cereal, it's a new serialization library that supports xml, json and binary formats. I looks fairly easy to use though I haven't tried it myself, I just saw it pop up on isocpp.org.

When working with binary files, you also need to pay attention to endianness.


Kinda sorta.

All PCs are x86 (little-endian), two of the three major game consoles this generation are x86 (XBone and PS4), the vast majority of mobile devices and gaming handhelds are ARM-based (bi-endian technically, but to my knowledge iOS/Android/WP/DS/Vita all run in little-endian mode), leaving only the PPC devices out there (also bi-endian, but the WiiU runs in big-endian mode). Endianness was a major concern for gameas in previous generations of game hardware (Xbox360/PS3 were big-endian PPC), but not so much for new games code being started today. Them kids seem to have it easier with every generation. smile.png

Also note that ports typically do not allow sharing of save games from other ports, and especially not between PCs and consoles. The end result is that you don't really need to care about endianness at all for things like file saves.

Deal with endianness when you're forced to and ignore it everywhere else. No reason to waste mental energy and time on a hypothetical problem you're not actually running into.

Sean Middleditch – Game Systems Engineer – Join my team!

Thanks for all the help guys,
I think I will go with the binary and seeing that this is a school project(and hobby), we not thinking about porting for the moment and will just be for PC. so I want to try at this binary save "challenge". beside cereal is there any other lib I should look at?(Not that am complaining just wanted to know what options I have)

Protobuf is a library to consider when dealing with serialization / creation of custom file type, I personally use it for storing levels data. You just create a proto file with your class structure like this:




message InventorySlot
{
  required int32 itemId = 1;
  required int32 itemcount = 2;
}

message CharacterData {
     required string name = 1;
     required int32 health = 2;
     repeated InventorySlot inventory = 3;
}

then you use the generator to create your c++ classes (or other language, there is a lot of decent third party protobuf library / generator available : http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns ), and you can use the built-in SerializeToOstream function to save your objects content into a binary file.

Another library I used for save file in one of my RPG project is SQLite, which is a file based database library. No need to have a server or anything, just include the library in your project, and you can open a database file and use SQL command like it's a normal database. It's geared toward speed and small memory footprint. you need to know a little about SQL, but the advantage is that you can patch your save file really easily. I also played around with integrating an economy system, built-in sql function make it easy to do so.


Since a computer doesn't need to look at a text saying "slot 0", "slot 1", "health", etc..., you can just skip that part.
You can have a byte which has the slot count, and then a byte or two with health.

the order in which you read and write data can implicitly define whats being read/written:

write(player.health)

for a=0 to maxslots

write(item[a].type)

write(item[a].quantity)

read(player.health)

for a=0 to maxslots

read(item[a].type)

read(item[a].quantity)

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

This topic is closed to new replies.

Advertisement