This project has moved and is read-only. For the latest updates, please go here.


May 8, 2011 at 7:17 PM

What's a good approach to complete map/level xml serialization?

I currently have two problems with world serialization:
firstly my farseer bodies belong to various actors in the level which also need to be serialized as does their relationship to the farseer bodies, as their body references will obviously be invalid on deserialize 
secondly I am using userdata as a reference to said actors so that its easy to tell which fixtures and bodies belong to what actor. As userdata gets serialized, I'm effectively serializing actors with physics bodies many times over, depending on how many bodies they deal with.

My only idea at this stage is to modify farseer serialization to allow individual bodies to be serialized on their own and then serialize them when their actors are serialized, but this still leaves joints unhandled.

Has anyone dealt with a similar scenario? I have a feeling that there's a much better way to do this.

May 11, 2011 at 8:24 PM

The World serialization outputs the body userData with no issues that I have found. 

Reading it in has been a bit of a problem.  Right or wrong, here is what I did.

The object that I save in the body.userdata  has a key that uniquely identifies this object.    Since this object is my custom class and is not part of the Farseer assembly, ReadSimpleType can not resolve custom class. So I modified ReadSimpleType in the serialization class to raise an event and I deserialize it in my game code and store it in a Dictionary using the object’s Key.  ReadSimpleType returns the Key  so what ever is calling it can save this key in the usserdata.

Once the world  has been deserialized,  I just iterate through the body list and swap out the keys for the real object from the Dictionary.

Also,  I found that  deserialization was a bit slow on the phone.  I converted that aspect to use Linq. Was planning to share at some point.

May 14, 2011 at 5:00 AM

What are the keys and values for this dictionary?

May 14, 2011 at 2:20 PM

Here is a simple sample of the dictionary and data class that I use. 

public Dictionary<int, EntityData> entityDataList;

public class EntityData {
    public int key { get; set; }
    public float drawDepth { get; set; }
    public Body body { get; set; }
    public Vector2 origin { get; set; }
    // need texture name to hook up the texture after deserialization
    public string textureName { get; set; }
    // texture will not be serialized
    public Texture2D texture { get; set; }