How to realize saving/loading a game?

As mentioned in other threads, I’m relatively new to the field of 3D- and in fact stand-alone game development. So far I’ve been doing browser-based games (although done in Java) so the topic this thread is about never was of relevance to me.

So today I came across a question that sounds simple at first but induces a lot of complexities, especially to someone who hasn’t done it before: What’s the best way of implementing a save and load function?

Every game has to have it so there’s no way around it, but what’s the best format to save in? What parts of the game state should get serialized, which are rebuilt at load? Does JME3 offer helpers for managing this and if so, what’s the best way to design the data structures/game objects to go along with it nicely? Are there recommended resources covering this topic, maybe even JME3 specific ones?

Any idea or other input is highly appreciated!

jME3 has a Savable interface.

your beans can implement it (you have to implement a read an write method). you can then save the output to binary or xml.

That’s how we save and load j3o files, so every object of the scene graph (and many more) implements it, have a look at Spatial for example.

Thanks for the hint.

I still got two (and a half) questions left:

1.) What’s the intended use? Make sure all game objects implement Savable or inherit from Spatial and then use the appropriate Exporter on the root node?


a.) If I’m not mistaken, the current logic also saves the meshes of geometries. So if I use a lot of custom meshes I’d have to implement an own logic to check whether the saved mesh is still the correct one?

b.) Textures and Models are probably not saved this way but are referenced by their name so that changes to them are reflected automatically after loading, right?

Thank you for your replies.

  1. For example, you dont have to save the whole scenegraph if you dont want to, just create other objects that implement the savable interface.

    2a) What do you mean “still the correct one”? It will be loaded as it was saved.

    2b) Textures yes, for Spatials this is only true if you use an AssetLinkNode, otherwise its data is saved completely in the j3o.

This is quite an interesting question and a “hard-to-explain-clearly” one.

I’ve read somewhere about this problem, may be an article in a book of Java Game Dev.

I’m not good at “explain thing” and …English too, but I will try to give you some of my ideas…

So, the answer depend on what kind of game is your game…?

If it’s a …

+Puzzle one: with some piece of triangle, or some crosswords, or rows of tertris → You have to save the “shape” of the process : which block player build or destroy, the word they fill in crossword…

+3d Racer : You save the position of the player’s car, the fuel he has left , the position of the opponents … Maybe the condition of the road (damaged, obstacle’s positon change by car’s hit…) , the enviroment (rain, snow…)

+3d RPG : You save the attribute (strength, intel , ammor …) of the player’s (1 or a team) , the position , the flags of every trigger that they pass, every NPC’s they talk and the attribute of them, every quest they done and every event they came across… You DON’T save the monster which is dead, the monster which is not a clue in the future, or not related any more with player…

+3d shooter: You save the (health , bullets , position…) And as above you don’t save DEAD one…!


The QUESTION is : why you save some thing and don’t save something …


Things you save is for 2 purpose :

  • By loading them, they are considered the elements of the process (the game) or some objects (player,NPC,quest…). By reading their values, you can compute and re-struct (completely fill) the original one! So only needed thing must be save, other things can be compute don’t save.
  • In other hand, some thing very hard to re-compute, so you save them although you can compute them in about 2hours or more :smiley:

    So, If you have

    a Map/Level: (in XML maybe like Ogre scene) which contain your objects (player,NPC,Trees,…) have their initial attributes (pos,scale,material,light,animation___health, strength___ cost,damage…), you only have to save the changed.


    The next QUESTION : how can we save-load “things” in java Game.


    Serialize method can be explain more carefully in some other paper but the main idea of it is:

    1 - (Save) Write to a (XML/binary) file attributes of (1,many) object,

    ///////in the write method of Serialize interface you’ve implement for the Object :

    ////////////////// calll write method of every attribute which also implement Serialize…

    ///////////////////////////////// others don’t implement Serialize , save them your way and remember all the orders.

    2 - (Load)

    a)Read from file by the orders you just make!

    b)Re-compute by the essential attributes to fill a full-attribute object!

    By the way, “re-compute” means:

    you combine

    . the info that you save it the file

    (+) with exits-and-unchange info you have in your map.

    (+) your rule about things in your game…

    (+) physics , AI and something else…

    to have : the result!!!

    (Example :

Position = "On a mountain"
Goal = "Kill a wolf"
ActionNow = "Attack"
Target = "Wolf"
Animation ="Attack_ani1"
-> place the model "Hero.mesh" on the mountain and let it play the "Attack_ani1" animation head to "Wolf"

The next QUESTION : XML - binary or something else????
You may see the point but you 're confused about what format to be chosed...

In my opinion , XML is a good choice for JME and for intergated with JMP too, so you can write a very simple plugin in JMP to read and edit your(maybe- player/weapon/quest/skill...- with GUI).

So for the size compress , you can re-code you write method, I don't think it a long and hard task!

The next QUESTION : What in JME already can help us or we have to write from scratch

I have came across this problem before and I think JME is mainly a "scene-graph" graphic engine , not currently "full-game engine" so something you have to code yourself is :

I) Load all assets in your map: (Scene, Models, Sound, Database...)
II) game loop :
1.Do AI,physic,decision
2.Update everything
3.Draw everything
4. Catch input of user -> Can take them to (Save/Load) -> Save them to (XML,binary,database..)
5. If input is invalid or players do invalid actions -> don't let them do it!
6. More function
7.Update New change.
End loop!
III) If game end of player want to quit, high score and say goodbye!

Maybe it not show you any thing... :D but If you need I can share some ebook about this problem and the author have his solution (and sample code) and my implement work in my case :D

Good day!
1 Like

One more technical question concerning JME’s Savable interface: Do my implementing classes need to provide an empty constructor?

1 Like