Jump to content
  • Advertisement
Sign in to follow this  
flashinpan

Help with Serializing Jagged Array in C#

This topic is 4559 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

Here is my updated code, trying to implement a jagged array:
     [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];

          [XmlArray("gw")]
          public GameObject[][] world_objects = new GameObject[MAX_ROWS][];

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

               world_objects[0] = new GameObject[10];
               world_objects[1] = new GameObject[10];
               world_objects[2] = new GameObject[10];
               world_objects[3] = new GameObject[10];
               world_objects[4] = new GameObject[10];
               world_objects[5] = new GameObject[10];
               world_objects[6] = new GameObject[10];
               world_objects[7] = new GameObject[10];
               world_objects[8] = new GameObject[10];
               world_objects[9] = new GameObject[10];

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

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

     [XmlRoot("GameObject")]
     public class GameObject
     {
          [XmlElement("it")]
          public ITypes icon;

          public GameObject()
          {
          }          
     }

     [XmlRoot("Robot")]
     public class Robot : GameObject
     {
          [XmlElement("own")]
          public string owner;
          [XmlElement("ise")]
          public bool is_enemy;
          
          public Robot()
          {
          }

     }

     [XmlRoot("MasterRobot")]
     public class MasterRobot : Robot
     {
          public MasterRobot()
          {
               if(!is_enemy)
               {
                    icon = ITypes.FriendlyMasterRobot;
               }
               else
               {
                    icon = ITypes.EnemyMasterRobot;
               }
          }
     }




Here is the resulting XML file:
 <?xml version="1.0" ?> 
- <World xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <a>5</a> 
- <gw>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  <GameObject xsi:nil="true" /> 
  </ArrayOfGameObject>
  </gw>
  </World>



How can I get this code to the point where it will output the jagged array contents when they are not null? If I try and assign an actual Object to the array I get an error: "MasterRobot" not expected. handled Exception: System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type robotsgame.MasterRobot was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

Share this post


Link to post
Share on other sites
Advertisement
It works this way:

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



but it does not work this way:

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


or this way:

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

Share this post


Link to post
Share on other sites
I have a guess, but I dont know any C#, so they are based on absolutely no solid knowledge.

Don't you need to put down this:

class something : public baseClass
{
//stuff here
};

for class inheritance?


It seems like the program is having trouble realizing that robots, n' such actually ARE GameObjects. Try writing some simple stuff with your classes to make sure everything is actually being inherited.

(i.e. try making a simple pointer to a GameObject and assign it a Robot of some sort.)

Share this post


Link to post
Share on other sites
They removed the access specifier in the inheritance list. Kind of nice, since 99% of the time, everyone used public inheritance in C++.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!