WP7 Game Development : A Beginners Guide (XML Specified Content)

Hello again. I am here today to talk about setting up objects in XNA using data read from .XML files. There are a number of ways to achieve this. One of the most popular is to use the built in XML serializer. I will be talking about this in one of my next articles. Today I am going to show you how to use the Content Pipeline to load object properties from an XML file.

 

When using this method all your XML files are converted into .xnb format and compiled into the project. This allows for more efficient loading and hides the data from tampering. If you have a lot of data that does not need to be altered by the application this is a very efficient way of loading it. For my last game I used this method to load all the level layouts and the hints which would appear between levels. For my new game I am using it to define particle effects. This cuts down on the use of polymorphism which leads to further efficiency.

 

Anyway, for this to work, all the properties of the class which you are loading to must be public and must all be given default values. They must also have the same names as the elements in the XML file. I’m fairly certain there is a way of using shorter element names in the XML file by putting something like [XmlElement(ElementName=”name”)] above the class property you want specified by the element “name”. This would be useful in shrinking large file sizes.

 

The process of using the content pipeline for loading XML files in a Windows Phone 7 project may seem complicated at first but it is not once you have done it. It requires the addition of two new projects to your solution. One Windows project for the Content Pipeline to read from and one WP7 project for your application to read from. These will actually be copies of each other and will share the same files.

 

The process of setting up your custom content is as follows:

 

Step 1: Add a  project to your solution:

 

Step 1

Step 1

 

Right click on your solution in solution explorer and select Add->New Project…

 

Step 2: Create a Windows Game Library project:

 

Step 2

Step 2

 

Select Windows Game Library from the list. Name it “CustomContent(Windows)” or something useful like that. Replace the automatically generated “Class1.cs” class with your class or change the namespace to match the rest of your solution and use it.

 

Step 3: Create a copy of the Windows project for the phone.

 

Step 3

Step 3

 

Right-click on the Windows Library project you have just created and select “Create Copy of Project for Windows Phone”. Change the name of the project created to something useful like “CustomContent(WP7)”. If you try to open a class contained within both of these projects from each project you will notice it is a shared file. This is what you want. I would advise working on any shared classes from within the WP7 project as I have encountered issues of this project not building when making changes from the Windows project.

 

Step 4: Adding the necessary references to these projects:

 

Step 4

Step 4

 

In the Solution Explorer, right-click on the references folder within your main project and select “Add reference…”. From here select the “Projects” tab and select the WP7 project you added earlier.

Then right-click on the references folder within your main content project and select “Add reference…”. From here select the “Projects” tab and select the Windows project you added earlier.

 

Step 5: Add any custom content:

 

To add XML files to your solution, right-click on your main Content project and select “Add->New Item…”. Then select XML file and give it the name you want. You can add your custom classes which you wish to be able to load to into either of the projects you added earlier (They are shared). I have included an image of a matching class and XML file with the standard formatting and an XML file defined for array specification. (Desteroids is the namespace).

 

Class and XML layout

Class and XML layout

 

To specify array content you just add [] brackets at the end of the type and present each array entry between <Item></Item> tags:

 

XML array layout

XML array layout

 

The content is then used like any other Content Pipeline entity. To use the content importer in a class you must add the following using statement:

using Microsoft.Xna.Framework.Content;

The code to load a ParticleEffectSettings (as shown above) object is:

ParticleEffectSettings thrusterfireSettings = contentMgr.Load<ParticleEffectSettings>("XML file path-name here");

The code to fill an array of ParticleEffectSettings (as shown above) objects is:

ParticleEffectSettings thrusterfireSettings[] = contentMgr.Load<ParticleEffectSettings[]>("XML file path-name here");

 

For anyone interested in more reading, here are a couple of useful links on the topic:

A guide to creating a 3D particle system for XBOX 360 using XML. Same process as above but not as beginner friendly and shows how to load non-standard data types into your class.

A really good introduction to XML. This is a very good description of the hows and whys of XML. It is not specific to XNA but is relevant to anyone using XML. It is very long though.

 

I hope you found this helpful.

Thanks for reading.

 

About the Author

Keith Cully

Keith is a dedicated, hardworking individual. Although he started out as a bespoke furniture maker, hard times and a huge interest in technology led him to become a computer games developer (I know it sounds random). Keith is currently attending the Computer Games Development course at I.T. Carlow and has skills in a number of languages. Keith is especially interested in mobile platforms and is currently developing for Windows Phone 7. Follow me on twitter for updates or check out my website.

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*