views:

92

answers:

5

I'm making a game in Java, and I need a good file format to store the player's saved data.

Any suggestions?

Feel free to give examples in code if you want.

EDIT: I don't want it human-readable and this is a server-client game, so the saved data will be on the server's machine. Also, I don't want to use Serialization.

+1  A: 

XML

let's you save any data structure you may have in a standard format and you won't need to write your own parser/writer for it

if you need the files to be "secured" from gamers changing their scores/progress/... (not sure where the files are stored? or whether this matters?) you could pass the XML through an encryption algorithm or encrypt the data elements before putting them into the XML

drfrogsplat
+1  A: 

Since its your game, you could define a file format yourself. For saving game's state directly serializing and storing will do. As for the players saved data (which could be his level progress, game score etc) you could use XML.

<?xml version="1.0"?>
<Game>
<player  id="01d">
     <name>
             John
      </name>
      <skill>
        Rookie
      </skill>
      <score>
       122
      </score>      
</player>
</Game>

Ofcourse you could encrypt it to make it hack-proof

Vivek Bernard
Encrypting probably wouldn't do any good, since you'd need to have the key available somewhere the user can read it.
Brendan Long
+3  A: 

Depending on how complex the information is that you want to save you might find sqlite to be very useful. The database is saved as one file and you can access it using SQL Queries. Its super lightweight and if you move the game to android, I think most user data is stored that way anyway.

If sqlite isn't you cup of tea, I would use xml. its very standard and Java has a library for parsing it. If you need to low-level encrypt some data or if you have binary data like an image, maybe base64 that data and throw it into a CDATA tag.

here is an example that I found on another stackoverflow post that uses CDATA/base64:

<?xml version="1.0" encoding="windows-1252"?>
<mediafiles>
  <media media-type="image">
    <media-reference mime-type="image/jpeg"/>
    <media-object encoding="base64"><![CDATA[/9j/4AAQ[...snip...]P4Vm9zOR//Z=]]></media-object>
    <media.caption>What up</media.caption>
  </media>
</mediafiles>
DJTripleThreat
I might go with either XML or some gzip'd binary format.
Gnarly
good luck with your game! :)
DJTripleThreat
Actually it's an emulator for a game, but yep - thanks!
Gnarly
After reading what Ron Savage said, I might use some type of SQL database.
Gnarly
+3  A: 

All of the answers so far seem to be about XML, which isn't a bad format, but there are other options you might find useful, which should make your startup times faster:

Json: Common and has been around longer than my next two suggestions.

Thrift: What Facebook uses. Should be faster than Json, supported by less languages.

Protocol Buffers: Used by Google. Probably the fastest, and also easy to extend.

Or just make your classes support Serializable.

Brendan Long
+1 All of those are good ideas, especially making your class serializable.
DJTripleThreat
+2  A: 

If as you say, the information will be saved on the server it should definitely be in a database. The typical game protocol is store the user information in a database - pull it into memory when they login, with lazy updates to the db for changes to the object in memory (keeps game performance high) as they play the game and update their "in memory" user game state object.

Don't limit the scalability of your game by starting with file based storage just because it might be slightly easier.

Ron Savage
You know, now that you put it that way - you're right. A database would be more proper as it's a base of data (the saved files). Hmm.
Gnarly