Sign in to follow this  

[C#] [XNA] How can I correctly serialize this barrier class?

Recommended Posts

When saving my level data I am currently saving an array of tiles. The tile class looks like the following:
    public class Tile3D
        public Barrier[] barriers;
        public Vector3 position;
        public float size;       
        public Tile3D_Type type;
        public Tile3D()
            barriers = new Barrier[6];

The problem arises when I come to serialize/deserialize the barrier class. The barrier can be created from a quadrangle as follows:
    public class Barrier : IOctree_Object, IGUI_Object
        RigidBody3D body;      
        Quadrangle quadrangle;
        Vector3 scale;
        List<CD_Triangle> triangles;

        public Barrier(Quadrangle quad)
            quadrangle = quad;

            Vector3 s = new Vector3();
            s.X = quadrangle.Width;
            s.Y = quadrangle.Height;
            s.Z = 0;

            scale = s;
            triangles = new List<CD_Triangle>(quadrangle.Triangles);

            body = new RigidBody3D(quadrangle.Position, RigidBody3D_Type.Static);


The quadrangle class serializes with no problems but because the Barrier class requires a Quadrangle to be created properly, it can't be deserialized due to it not having a parameterless constructor. How can I serialize/deserialize the barrier class correctly knowing that I must have a parameterless constructor, and that I can create the barrier from a quadrangle?

Share this post

Link to post
Share on other sites
Quick solution would be:

Mark the quadrangle field as public and add a parameterless ctor
Additionally i'd suggest to move all the logic code into an Init() method and out of the ctors.
It's tempting to use ctors beyond initializing fields and properties, but in the long run it turns out to be quite messy. At least from my experience.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this