2
\$\begingroup\$

I want to save my game in libgdx, and I have no problem using Kryo to save everything, except the box2d Worlds and the RayHandlers from box2dlights - these are not Serializable and do not have default constructor, ruling out most of the methods. I have no idea how to save them, but I'm aware the most used approach is JSON - which I'm ok with. I even found this, but it seems to be specific for jBox2D alone, because I'd have to change all the names and packages in the code to match libgdx's box2d version's. Also, it uses the famous original JSON lib for java, whereas libgdx has it's own internal JSON handler and parser. So, I could just convert everything, as I said, it's all opensource. But it is such an absolutely essential task (serializing), present in every single game, that I think libgdx might also have something to do this. I can't, however, find anything about it, anywhere. Can someone put a light on this?

Just some background: my game happens in a galaxy, and every single planet has it's own World for handling. But I only load one planet at a time in the memory: when the player leaves, I save it on a file. So my save consist of one main file with player data, galaxy structure, anyway, everything else, and a folder with one file for each Area (planet's map). This map contains some objects and data plus the World and RayHandler from box2d and box2dlights. I'm using JSON (libgdx's default) already for the Area class, but if the world and rayHandler fields are no transient, I get a StackOverflow, probably due to circular refences not supported by JSON writers. As I said, I can't use kryo due to the lack of private default constructors in most classes, and JSON does seem to be the standard approach for this. The lack of a solution built-in in libgdx, however, is really making me wonder what I'm not seeing in all this.

\$\endgroup\$
2
  • \$\begingroup\$ Sidenote: Do you need to save everything as it is in memory, or can you save just the important data, eg for example the data used when you create a planet (presuming you generate planets) \$\endgroup\$
    – ThorinII
    Commented Dec 5, 2013 at 2:17
  • \$\begingroup\$ I need to save everything, including the position of the bodies in the world, and their current state, as the player can save anytime, and, when he loads, the game must be as he left. \$\endgroup\$
    – Luan Nico
    Commented Dec 5, 2013 at 9:18

1 Answer 1

1
\$\begingroup\$

For most scenarios it's beneficial to wrap the objects you have no control over (box2d in your case) in your own objects with custom serialize methods.

That or build a custom serialize method in your world manager presumably you have a list of the bodies somewhere (for destroying later), which you could flatten down in a serialize method for the whole world instead of per-body.

But either way you'll need your own serialize method, decide which attributes you need to keep most times you just need position and angle, but if you have other non-constant properties like restitution, etc. Save those as well.

Here is some example on custom methods: https://github.com/EsotericSoftware/kryo#kryoserializable

Same goes for the Lights in box2dlights, and i don't think the rayhandler has any state to save, so long as you successfully re-add the lights once they are de-serialized the rayhandler should be fine.


Though as a side bar it seems that kryo may be able to serialize these private fields in Box2d via reflection: https://github.com/EsotericSoftware/kryo#fieldserializer

For your consideration, though i don't have much experience with kryo so i cannot comment.

\$\endgroup\$
2
  • \$\begingroup\$ I can't use kryo (or any other library like that) because some of the classes don't have default (no-args) constructor (absolutely required for reflection based implementations). I ended up changing the code of the iforce2d approach; since I don't use some things like joints and multiple fixtures per body, I could simplify it a lot and translate everything relevant (it's a very specific change, though, so it won't make sense to share it), and in the end it was basically just doing what you said: making my own serialization mechanism... I just find it very odd that there's nothing built-in... \$\endgroup\$
    – Luan Nico
    Commented Jan 9, 2014 at 23:45
  • \$\begingroup\$ Yeah, its a bit confusing that at least the libGDX box2d stuff isn't out of the box (or at least close) to interoperable with Kryo considering Kryo was made by one of the 2 libGDX guys haha. \$\endgroup\$
    – Smanger
    Commented Jan 10, 2014 at 21:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .