Serialization

Started by
4 comments, last by flashinpan 18 years, 3 months ago
Can someone shed some light on why the 2D GameObject array will not serialize?

[XmlRoot("World")]
	public class World
	{
		[XmlElement("maxrows")]
		public const int MAX_ROWS = 10;
		public const int MAX_COLS = 10;

		[XmlElement("a")]
		public int a = 5;

		[XmlIgnore]
		public Image[] images = new Image[9];

		//[XmlIgnore]
		public GameObject[,] world_objects = new GameObject[MAX_ROWS,MAX_COLS];

		public World()
		{
			//InitImagesHash();
			InitImages();

			for(int row = 0;row < MAX_ROWS;row++)
			{
				for(int col = 0;col < MAX_COLS;col++)
				{
					world_objects[row,col] = new MasterRobot();
				}
			}

			world_objects[5,5] = new Wall();
			world_objects[2,2] = new Wall();
		}

		public void InitImages()
		{
			images[0] = Image.FromFile(MasterImageList.FriendlyMasterRobot);
			images[1] = Image.FromFile(MasterImageList.EnemyMasterRobot);
			images[2] = Image.FromFile(MasterImageList.FriendlyMedicRobot);
			images[3] = Image.FromFile(MasterImageList.EnemyMedicRobot);
			images[4] = Image.FromFile(MasterImageList.FriendlyEngineerRobot);
			images[5] = Image.FromFile(MasterImageList.EnemyEngineerRobot);
			images[6] = Image.FromFile(MasterImageList.FriendlyDestroyerRobot);
			images[7] = Image.FromFile(MasterImageList.EnemyDestroyerRobot);
			images[8] = Image.FromFile(MasterImageList.Wall);
		}


		public void LoadGame()
		{

		}

		public void DisplayWorld(Graphics gr)
		{
			for(int row = 0;row < MAX_ROWS;row++)
			{
				for(int col = 0;col < MAX_COLS;col++)
				{
					//gr.DrawImage(((Image)images[world_objects[row,col].icon]),col * 32, row* 32);					
					gr.DrawImage(images[(int)world_objects[row,col].icon],col * 32, row* 32);					
				}
			}
		}		
	}


Advertisement
I'm assuming GameObject is a base object from which you derive other objects?

You need to tell the C# XML Serializer what derived objects are in your array of GameObjects. Take a look at the XmlArrayItemAttribute and similar topics. For every derived object in the GameObject array you have to list its type in an XmlArrayItemAttribute structure.

I'm a little foggy on the subject though, and don't have my code handy for reference. Really hope this steers you in the right direction :)
Are all of the GameObjects serializable themselves?
Turring Machines are better than C++ any day ^_~
Here is the error I get:


Unhandled Exception: System.InvalidOperationException: There was an error reflecting type 'robotsgame.World'. ---> System.NotSupportedException: Cannot serialize object of type robotsgame.GameObject[,]. Multidimensional arrays are not supported.




Multidimensional arrays are not supported. ?????
apparently jagged arrays are supported, but not rectangular arrays?

How do I declare a jagged array and how is it different from a 2D array?
I have this working now...thanks.

What I mean is...no need to spend more time on this question.

This topic is closed to new replies.

Advertisement