Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Conny14156   Members   -  Reputation: 268

Like
0Likes
Like

Posted 09 April 2014 - 07:44 AM

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). 

 

 



Sponsor:

#2 DiegoSLTS   Members   -  Reputation: 1373

Like
0Likes
Like

Posted 09 April 2014 - 08:12 AM

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).


Edited by DiegoSLTS, 09 April 2014 - 08:15 AM.


#3 irbaboon   Members   -  Reputation: 861

Like
0Likes
Like

Posted 09 April 2014 - 08:27 AM

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.


Edited by diventurer, 09 April 2014 - 08:28 AM.


#4 LennyLen   Crossbones+   -  Reputation: 3782

Like
1Likes
Like

Posted 09 April 2014 - 09:22 AM


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.



#5 Sunda   Members   -  Reputation: 334

Like
0Likes
Like

Posted 09 April 2014 - 10:11 AM

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.



#6 Lantre   Members   -  Reputation: 184

Like
1Likes
Like

Posted 09 April 2014 - 10:53 AM

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.



#7 SeanMiddleditch   Members   -  Reputation: 5803

Like
2Likes
Like

Posted 09 April 2014 - 01:03 PM

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.

#8 Conny14156   Members   -  Reputation: 268

Like
0Likes
Like

Posted 09 April 2014 - 01:44 PM

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)



#9 dlan   Members   -  Reputation: 408

Like
1Likes
Like

Posted 09 April 2014 - 02:40 PM

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.


Edited by dlan, 09 April 2014 - 02:43 PM.


#10 Norman Barrows   Crossbones+   -  Reputation: 2125

Like
0Likes
Like

Posted 10 April 2014 - 07:59 AM


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 1988"

 

rocklandsoftware.net

 


#11 Norman Barrows   Crossbones+   -  Reputation: 2125

Like
1Likes
Like

Posted 10 April 2014 - 08:02 AM


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

 

only if you change hardware


Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 


#12 Stainless   Members   -  Reputation: 944

Like
5Likes
Like

Posted 10 April 2014 - 08:54 AM

Always, always, always start with a version number.

 

I cannot tell you how many times I have had a bug report saying "load game broken". When I look at it the tester has tried to load a saved game from an earlier version of the game, something is in the wrong place and everything goes bang.



#13 frob   Moderators   -  Reputation: 21269

Like
3Likes
Like

Posted 10 April 2014 - 03:43 PM


Always, always, always start with a version number.

 

Version numbers for the file, version numbers for the structures, version numbers for everything.

 

In quite a few projects I've seen a whole bunch of load functions, much like a factory. First it loads the version number, then a switch statement full of functions named LoadV1(), LoadV2(), LoadV3(), LoadV4(), and so on.  Also popular are a chain of functions ConvertV1toV2(), ConvertV2toV3(), ConvertV3toV4(), and so on.


Check out my personal indie blog at bryanwagstaff.com.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS