BinaryFormatter not serializing field

769 views Asked by At

I am having a little trouble getting BinaryFormatter to actually serialize this one field.

I have a Tile class and I serialize it's position and ID, I then save all these tiles using the BinaryFormatter. Every one other field is non serialized and is initialized using the value of the ID.

However, the ID field just won't serialize but everything else does. What could be the cause of this?

Here is my Tile fields:

        // The ID of this tile
    private int id;
    // The name visible for the tile in the map editor
    [NonSerialized] private String name;
    // The file location for this tile for loading a texture
    [NonSerialized] private String fileName;

    // Sprite for drawing this tile
    [NonSerialized] private Sprite sprite;
    private Transform transform = new Transform();

    // If this tile is blocked, blocked tiles can not be walked over are classes as "unpassable"
    [NonSerialized] private bool blocked;
    // Used to determine if a specific tower types can be placed on this tile, if any
    [NonSerialized] private short towerMask;
    // The tower that is placed on this tile, if any
    [NonSerialized] private Tower tower;
    // A path generated for enemies to navigate from this tile to another, used for tiles that can spawn enemies
    [NonSerialized] private Path path;

When I load the map, I do this:

            map = MapSerializer.Load("test");

And the MapSerializer class is just the usual setup, with a static load and save method like so:

        public static Map Load(string filename){
        Map map;
        String path = Path.Combine("maps/", filename+".mp");
        BinaryFormatter formatter = new BinaryFormatter();

        FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);

        try{
            Trace.WriteLine("Loading map from " + path, "Map Serialization");
            map =  (Map) formatter.Deserialize(stream);
            Trace.WriteLine("Map Loaded!", "Map Serialization");
        }
        finally{
            stream.Close();
        }

        return map;
    }

The other annoying this is, when the tiles are loaded it does not call the no args constructor. What is with that? I have to manually loop through all the tiles and call Create(), despite it is called in the no args constructor.

Any ideas that one int field is not writing to disc?

EDIT:

Serialization code:

 public static void Save(Map data, String filename){
        String path = Path.Combine("maps/", filename + ".mp");
        BinaryFormatter formatter = new BinaryFormatter();

        if (!Directory.Exists("maps"))
            Directory.CreateDirectory("maps");
        FileStream stream = File.Open(path, FileMode.Create);

        try{
            Trace.WriteLine("Writing map to " + path, "Map Serialization");
            formatter.Serialize(stream, data);
            Trace.WriteLine("Map Saved!", "Map Serialization");
        }
        catch (SerializationException e){
            Trace.WriteLine("Failed to save map : Error - " + e.Message);
            throw;
        }
        finally{
            // Close the file
            stream.Close();
        }
    }
1

There are 1 answers

1
MaLio On

You have not posted the serialisation code. To your second issue ...

The serialisation will not call your default constructor. If you implement ISerializable then the constructor taking the serialisation specific arguments will be called.

An alternative is to implement System.Runtime.Serialization.IDeserializationCallback. For details see here link.

Another alternative is by decorating a method with the OnDeserialized attribute.