Jump to content
  • Advertisement
Sign in to follow this  
flashinpan

Serialization

This topic is 4582 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);					
				}
			}
		}		
	}


Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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. ?????

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!