App Hub
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (3 posts)

How to serialize a class containing references, using XmlSerializer?

Last post 2/10/2010 8:20 PM by MJP. 2 replies.
  • 2/10/2010 2:12 PM

    How to serialize a class containing references, using XmlSerializer?

    I am trying to serialize my player data and save it to an xml document. Inside my player class, I have a list of skills.

    private List<Skill> m_skills = new List<Skill>(); 

    My skill class branches into specific skills such as MeleeAttack. These classes contain references to the player data. For example I have a stats class within my player class.

        public class MeleeAttack : Skill 
        { 
            #region VARIABLES 
     
            PhysicsObject m_physics; 
            InputStates m_movement; 
            Stats m_stats; 
            Orientation m_orientation; 
            #endregion 
            #region CONSTRUCTORS 
     
            public MeleeAttack() 
            { 
            } 
     
            public MeleeAttack(InputStates movement, PhysicsObject physics, Stats stats, Orientation orientation) 
            { 
                m_movement = movement; 
                m_physics = physics; 
                m_stats = stats; 
                m_orientation = orientation; 
            } 
            #endregion 

    When I serialize my player, the xml document just stores the list of skill types, not the data within them. So when I load my game, the skills are remade using the empty constructor, and the variables are all null.

    I searched Shawn Hargreaves' blog again, and I could only find an example to serialize shared data using the IntermediateSerializer, and I am using the XmlSerializer.

    Is it possible to properly serialize the references in this class, or is this just a poor choice of design?
  • 2/10/2010 6:15 PM In reply to

    Re: How to serialize a class containing references, using XmlSerializer?

    Dayvan Cowboy:
    Is it possible to properly serialize the references in this class, or is this just a poor choice of design?

    I'm not sure if it's possible or not, but I'm inclined to go with "poor design choice" simply because XML is relatively inefficient for what it looks like you're doing. It's great for item stats and other things that will be compiled, or if you want to have user-moddable content, but if it's just used to save data (especially complex data such as physics) it could very easily balloon in size.

    Without seeing the rest of your game in context it's just a guess, but it looks like you're saving some data that's not needed. Are you using JigLibX and saving out the physics object's states? I don't think that it's necessary, nor would it be wise due to the amount of data contained in them!

    What I would do is boil down your skill list to a set of small variables such as type, strength, experience, and whatever else you need, and recreate them when loading for the physics data. You can either serialize this list without much penalty, or use BinaryReader/Writer for maximum efficiency and speed.
  • 2/10/2010 8:20 PM In reply to

    Re: How to serialize a class containing references, using XmlSerializer?

    Three important things you need to know about XmlSerializer:

    1. It always uses the default constructor.
    2. It won't serialize private data. For something to get serialized, you need to make it accessible as a public property or field.
    3. It will serialize references, but always as a copy. Basically what this means is that each time the serializer hits an object that has a reference to another serializable object exposed as a public property or field, it will write out the all of the public properties/fields of the object that reference points to. This is a problem if you have the same object referenced multiple times by something you're serializing. For instance...say you had two MeleeAttack's referencing the same PhysicsObject. After deserialization, you'd end up with your MeleeAttack's having references to different PhysicsObject's with identical properties. I used to get around this by giving certain types of objects a unique integer ID, and then having the objects serialize the ID rather than the reference. Then after deserialization I'd have another initialization step where I'd use the ID to look up the corresponding object and grab a reference.
Page 1 of 1 (3 posts) Previous Discussion Next Discussion