Best way of loading player data?

I’m in the middle of making a platformer game, and I’m wondering what the best way of loading/saving levels and player data would be.
Any suggestions? I mainly just need to know what I am theoretically supposed to do. I’ve never ran into having to read and load files, most of what I’ve done have just been ‘play-through once and see how long it takes till you die’ games, like Mario, for example. ;D

All help is appreciated. :wink:

Hm, ok, to tell you the truth, maybe would be a good idea using Json plus, State Machine Logic.

I made a little drawing for you, took me some time but, i hope it helps :smiley:

http://img211.imageshack.us/img211/2439/statemachine.jpg

You can use an XML parser like SAX or DOM to save attributes of a character. At startup, you go through the file and assign the player variables based on the attributes inside the file.


<player>
<attribute name="health" value="100"></attribute>
<attribute name="mana" value="55"></attribute>
<attribute name="xpos" value="102.3"></attribute>
<attribute name="ypos" value="53.0"></attribute>
...
</player>

Thats pretty much how I do it in my game. Also if you are using libgdx there is a very simple method of doing this, but if not this should be helpful: https://github.com/libgdx/libgdx/blob/master/demos/superjumper/superjumper/src/com/badlogicgames/superjumper/Settings.java

Thank you! I will go try that now. Luckily I have basic knowledge of XML, I should have thought of this before ;D

Just an added extra point onto the previous answer.

If you wish to have a more complicated amount of info (for example, saving a players inventory) then the XML format would be difficult. The way you could do it is to make a class called something like “PlayData” and then save it as an object. Like so:

	public void saveData(PlayData pd) throws Exception{
        //The file it saves to
		File filePath ="saves";
		if(!filePath.exists()){
			//log.info("File path does not exist, creating it...");
			filePath.mkdirs();
		}//log.info("Got file path, attempting to get file...");
		File file = new File("saves/player.data");
		if(!file.exists()){
			file.createNewFile();
		}
		FileOutputStream save = new FileOutputStream(file);
		// Write object with ObjectOutputStream
		ObjectOutputStream saveOut = new ObjectOutputStream (save);
        //Now write it out
		SaveOut.writeObject (pd);
	}

And then to load it:

	public PlayerData getPlayerData() throws Exception{
	File filePath =new File("saves/player.data");
	if(!filePath.exists()){
		return null;
	}
	FileInputStream save = new FileInputStream(filePath);

	// Read object using ObjectInputStream
	ObjectInputStream saveIn = new ObjectInputStream (save);

	// Read an object
	Object obj = saveIn.readObject();

	if (obj instanceof PlayerData){
		return (PlayerData) obj;
	}else{
		return null;
	}
}

So long as everything inside the PlayerData class (including itself) implements Serializable, then this will work.

Hope I helped!

That is a really bad idea, mostly when the game is in a stage of development where classes change rapidly.
The standard serialization is a really bad idea to use for games, since it can break so fast.

Someone told me there’s a work-around for that, so it doesn’t break. Sadly, I forgot who/how.

I recommend either XML,Plain-Text,NBT or JSON.

  • Longor1996