Advertisement

C# XNA 4.0 How to serialize a 2D array?

Started by March 06, 2018 01:13 PM
44 comments, last by Shawn Naef 6 years, 9 months ago

I use an Int32 array myself to represent Tiles in a Map. Its a pretty decent system. I use MsgPack to do my serialization. Its incredibly easy to use. 

 

Serialize. I use a MapData struct which contains different information including the 2d array of Int32.


using (FileStream stream = new FileStream(filepath, FileMode.OpenOrCreate)) {

  var serializer = SerializationContext.Default.GetSerializer<MapData>();
  serializer.Pack(stream, mapData);
}

Boom. Done. 

Deserialize.


using(FileStream stream = new FileStream(filepath,FileMode.Open)) {

  var serializer = SerializationContext.Default.GetSerializer<MapData>();
  mapData = serializer.Unpack(stream);
}

 

If you need/have a map editor or something you can just add the MsgPack.dll to your editor project and as long as you use the same file extensions and data type it should work with no trouble. 

 

edit: added right code using MsgPack

You could Serialize your entire Save struct this was as well as long as your members are [Serializeable]. 

Wait one second. I grabbed the wrong code. That's System.Runtime.Serialization.Formatters.Binary serialization. One sec

Advertisement

Honestly though I know a fair bit about programming (Not beginner) but not exactly a pro. I have not used the binary system at all before. I do not even know what MsgPack is lol

What i know is mainly limited to C# XNA and ive been slowly adding to my knowledge i only recently made the breakthrough of learning how the draw and update loops work and then moved from hard coding to doing tilemaps (no redundant code). This form of saving files is all that i know at the moment. Also all save data is contained in a serialized struct look at my earlier post at the top.

Okay, well MsgPack is a library that handles Serialization the exact same way as Xml does (kinda) but instead of using Xml it uses a Binary Format. Its much smaller and faster. You can use whatever file extension you want. For instance all my Save files are SaveName.save 

 

Here is a link to their site: https://msgpack.org/index.html

 

Its really easy to use as well. You just install the Nuget or add the Reference directly and add a using statement at the top and then use the code I have above. Boom done. Its really that simple. It converts directly to and from your object. 

Just curious did you make your own UI system or using someone else's UI framework?

yeah that's not going to work with my setup I'm running an old version of studio 2010 to be compatible with XNA ;) the games will run on anything up to including windows 7 i know that eventually id want to learn monogame. i think the best way is like you  said to use the save game struct however 2d arrays i don't believe can be saved the way i want without pre-processing im essentially looking for someone that can my code modify the parts and show me how it works.

What .Net version are you using?

 

I use Monogame 

 

Advertisement

4.0

If you are using XNA 4.0 I believe that its targeting .Net 4, in which case you have access to System.Runtime.Serialization.Formatters.Binary Namespace. So you can use this. 

Serialize:


using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate)) {

	BinaryFormatter bFormatter = new BinaryFormatter();
	bFormatter.Serialize(stream, mapData);
}

Deserialize:


using (FileStream stream = new FileStream(filePath, FileMode.Open)) {

	BinaryFormatter bFormatter = new BinaryFormatter();
	mapData = (MapData)bFormatter.Deserialize(stream);
}

 

See my understanding is this is what I'm trying to do say we have a 2D array of 3x3 (3 arrays of  3 cells)

[0][1][2]

[1][2][3]

[2][3][4]

 

I'm trying to get

[0][1][2]

[3][4][5]

[6][7][8]

 

or

[Serialiazable]

[0]

[1]

[2]

.....

 

 

But try doing that with an array that is ---> 30 wide

by potentially 100s of thousands of levels down which would be alot of code.

 

use would look like this:


//Add the dll
using System.Runtime.Serialization.Formatters.Binary;

//Save the game to Disk
private void WriteSave(SaveGame saveGame,string filePath) {

    try {

        using (FileStream stream = new FileStream(filePath,FileMode.OpenOrCreate)) {

            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, saveGame);
        }
    }
    catch(Exception ex) {

        
    }
}

//Load the game from disk
private SaveGame LoadSave(string filePath) {

    saveGame = new SaveGame();

    try {

        using (FileStream stream = new FileStream(filePath, FileMode.Open)) {

            BinaryFormatter bFormatter = new BinaryFormatter();
            saveGame = (SaveData)bFormatter.Deserialize(stream);
        }
    }
    catch(Exception e) {

    }

  	return saveGame;
}

 

You don't have to worry about how its saving it. Its all in Binary. Its just a file that contains your data. It serializes to and from your SaveData object for you. I use this myself for the exact same thing. 

This topic is closed to new replies.

Advertisement