Share via


Loading Map/Game Data from XML, part 1

Over at 3D Buzz (<www.3dbuzz.com/>) there is an XNA class that is currently going: XNA 101. The class is currently going through the process of creating a text-based adventure game as it's "Hello World!" app - ambitious for beginners yes, but way more interesting.

Of course, this class is geared toward beginners so all of the game data is hard coded into the code. This got me thinking, what if people wanted to venture out and load this data dynamically. That's leads us to today's post: loading map/game data from an XML file.

So where does one begin? Well, the first place I like to start is with an empty XML file. ;-) But that's not terribly interesting to look at. So we'll create the basic structure here.

 <?xml version="1.0" encoding="utf-8" ?>
<Map>
  <StartingPosition X="1" Y="0" />
  <Areas>
    <Area>
      <Position X="0" Y="0" />
      <Title>Lounge</Title>
      <Description>This is where all of the lazy bums come and sit instead of working.</Description>
      <Items>
        <Item ID="ITM_COUCH" />
      </Items>
      <Exits>
        <Exit ID="DIR_EAST" />
      </Exits>
    </Area>
    <Area>
      <Position X="1" Y="0" />
      <Title>Entrance</Title>
      <Description>This is the main lobby for the building.</Description>
      <Items>
        <Item ID="ITM_PEN" />
      </Items>
      <Exits>
        <Exit ID="DIR_EAST" />
        <Exit ID="DIR_WEST" />
      </Exits>
    </Area>
    <Area>
      <Position X="2" Y="0" />
      <Title>Office</Title>
      <Description>This is one of the many offices located within the building.</Description>
      <Items />
      <Exits>
        <Exit ID="DIR_WEST" />
      </Exits>
    </Area>
  </Areas>
</Map>

What you'll see here is that our map data specifies some important data like the starting location for the player and details about each of the areas in the map. Within the areas are the IDs for the items and the directions (which are also stored in an XML file). We store IDs so that we can keep all of the data centralized in one location and not have to update it in many places.

I'm not going to really explain too much more about the XML file. You should create one that suites your own needs for your game.

Now that we have the XML file, we need a way to load that file into our game. We'll cover that next time.

Comments

  • Anonymous
    April 20, 2007
    David's Blog has some recent entries I would like to refer: Using C# for game scripts, part 2 Using C#