[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;
Vector3 scale;
List<CD_Triangle> triangles;

{

Vector3 s = new Vector3();
s.Z = 0;

scale = s;

}


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 on other sites
Implement ISerializable?

Share on other sites
Is that compatible with XML and XBOX 360?

Share on other sites
Quick solution would be:

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.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628354
• Total Posts
2982236

• 10
• 9
• 11
• 24
• 11