Home > C++, Programming > Loading Map/Game Data from XML (Part 3)

Loading Map/Game Data from XML (Part 3)

April 22, 2008

So far we have the backing map.xml file, we have the classes that we are going to use to store the data in our game, now all we need to do is to parse the XML file and create the instances. We could have tried to use the XML serialization support that is built into .NET, but the approach that we are going to use it much more flexible and easy to understand. Let’s see some code:

Code:

private Map LoadMapData() {
   StreamReader reader = new StreamReader(@"map.xml");
   XmlDocument mapDoc = new XmlDocument();
   mapDoc.LoadXml(reader.ReadToEnd());
   reader.Close();

   Map map = new Map();

   Vector2D startingPosition = ParseVector2D(mapDoc.SelectSingleNode("/Map/StartingPosition"));
    map.StartingPosition = startingPosition;

    foreach (XmlNode areaNode in mapDoc.SelectNodes("/Map/Areas/Area")) {
        map.AddArea(ParseArea(areaNode));
    }

    return map;
}

private Vector2D ParseVector2D(XmlNode node) {
    Vector2D vec = new Vector2D();

    vec.X = Int32.Parse(node.Attributes["X"].Value);
    vec.Y = Int32.Parse(node.Attributes["Y"].Value);

    return vec;
}

private Area ParseArea(XmlNode node) {
    Vector2D position = ParseVector2D(node["Position"]);
    Area area = new Area(position);
    area.Title = node["Title"].InnerText.Trim();
    area.Description = node["Description"].InnerText.Trim();

    foreach (XmlNode itemNode in node.SelectNodes("Items/Item")) {
        area.AddItem(itemNode.Attributes["ID"].InnerText.Trim());
    }
    foreach (XmlNode exitNode in node.SelectNodes("Exits/Exit")) {
        area.AddExit(exitNode.Attributes["ID"].InnerText.Trim());
    }

    return area;
}

I bet you thought there was going to be a lot more code than that, huh? =) As you can see, it’s pretty easy to parse an XML file. The code pretty much speaks for itself so I’m going to leave it at that. If you don’t understand something I suggest that you first dig into the MSDN library on XmlDocuments and XmlNodes so that you can try and figure it out, however, do feel free to post any comments/questions you have here as well.

You should be able to take this example and extend it to any XML file holding your game data now. Good luck! =)

-David

Advertisements
Categories: C++, Programming Tags:
  1. Shawn
    August 31, 2008 at 5:32 am

    Hello,

    Just came across these thanks to Google. I am currently trying to learn XML instead of using my own file format in 2D tile games. I have a question that maybe you can help me with. If I have this code:

    1 1 1 1 1 1 1
    0 0 0 0 0 0 0
    2 2 2 2 2 2 2

    in my XML file, is there a way to parse that line by line into a int[,] array?

  2. Shawn
    August 31, 2008 at 5:33 am

    Sorry, those numbers are wrapped in a Data node within the xml file. For simplicity I just posted a few numbers. Basically those are the indices for the texture to use in that particular cell. Any suggestions?

  3. August 31, 2008 at 10:40 am

    Line-by-line could be tricky depending on the XML parser that you are using, but you can definitely do something like that.

    If you have XML that looks like this it would be super easy:
    <Data>
    <Row>1 1 1 1 1 1 1 1</Row>
    <Row>0 0 0 0 0 0 0 0</Row>
    <Row>2 2 2 2 2 2 2 2</Row>
    </Data>

    You would just parse each row under the Data node and put that data into your array. If you need to create the array up front you could add some extra info in the Data part of the node like this:

    <Data Rows=”10″ Columns=”8″>

    </Data>

    Of course, you’d also have to split the contents of the row based on the delimiter that you used between the values in the row, in this case a space.

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: