Save/Load Game

This might sound simple, but really, how do I save or load a game? For example, a player passes through a checkpoint, the game saves the current state and when the player quits and restarts the game, he can load to the checkpoint.

use xml and store only the most important stuff, e.g player position, player gold / level / xp.

@tralala, thanks, you’re always there to lend a helping hand.

Hmm… I still can’t quite figure out how to get a String/Variable’s value from the XML. I have no problem working using XML files, but I can’t work out how to get/edit a value. jme’s help doesn’t help much neither.



Basically, for now all I want is that I get the value of String a from the XML file into my Java class.

You can also use Savable interface in JME3 and save/load some class with desired data.

I think I prefer using XML. If you can tell me how I can use them, I would appreciate.

myfile.xml :

[xml]

<manyCreatures>

<creature>

<name>myCreature1</name>

<hp>100</hp>

</creature>

<creature>

<name>myCreature2</name>

<hp>200</hp>

</creature>

</manyCreatures>[/xml]





[java]

public static void main(String[] args)

{

try

{

File file = new File("myfile.xml");

javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();

javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();

org.w3c.dom.Document doc = db.parse(file);

doc.getDocumentElement().normalize();

org.w3c.dom.NodeList properties = doc.getElementsByTagName("creature");

for (int s = 0; s < properties.getLength(); s++)

{

org.w3c.dom.Element propertyNode = (org.w3c.dom.Element) properties.item(s);

String name = get(propertyNode,"name");

int hp = getInt(propertyNode, "hp", s+1);

System.out.println(name+" "+hp);

}

}

catch (Exception e)

{

System.err.println("Incorrect xml file format");

e.printStackTrace();

System.exit(-1);

}

}





public static final int getInt(org.w3c.dom.Element element, String s, int DefaultValueOnFail)

{

String found = get(element,s);

if (found == null ) return DefaultValueOnFail;

return Integer.parseInt(found);

}



public static final String get(org.w3c.dom.Element element, String s)

{

return get(element, s, 0);

}



public static final String get(org.w3c.dom.Element element, String s, int i)

{

return get(element.getElementsByTagName(s).item(i));

}



public static final String get(org.w3c.dom.Node element)

{

if (element == null) return null;

return ( element.getChildNodes().item(0) ).getNodeValue();

}

[/java]

You can use both really, jME3 Savable system can serialize to xml

only thing you have to do is to implement the Savable interface for the objects you want to save, look at Spatial for an example. There are 2 methods read and write to implement that’s pretty straight forward.



then use the XMLExporter and the XMLImporter class to write an read files.

1 Like

What exactly is the jme Savable system? Thanks

For simple stuff like checkpoints you can also use AppSettings.load() and .save()

1 Like

Savable is kinda like Serializable in Java. Except that with Savable, you must manually define how and which stuff you want to save. Ok you can also use “transient” with Serializable, but apart from that, I don’t really know what the advantages are. Anyone care to explain? :slight_smile:

Thanks, anyone can tell me how to call them? I can’t find anything on the help or on here.

I have a question here:

The issue was saving and loading the game.

I want to save a players profile also, did jMP support for this?



What is the best thing I can do to save also the profiles name, etc.

Any hints there, thanks!!!

http://hub.jmonkeyengine.org/groups/jmonkeyplatform/forum/topic/as-i-keep-player-information/

glaucomardano said:
http://hub.jmonkeyengine.org/groups/jmonkeyplatform/forum/topic/as-i-keep-player-information/


it didn't satisfies the answers in my questions??

Can you give me a sample to save a player's profile name..

Thanks for helping.!!!

Yes, look at the SaveGame.java.

glaucomardano said:
Yes, look at the SaveGame.java.


I can't find it... Please supply the link of that code..

Thanks a lot!!!

Kindly supply the link that can I get it??

Thanks!

:smiley:

The link where can I get that source code…

SaveGame.java is nothing i found…

Please give me the URL… Thanks.