Content Importers (XNA Game Studio 4.0 Programming)

Using a file type that the content pipeline already imports is one thing; there are times when you have a completely new type of file to import (for example, a level in your game). So let’s create an example that shows off a concept such as that. First, create a new Game project.You’re actually going to create quite a few projects here, so add a new Content Pipeline Extension project to the solution as well that will hold your importer. Lastly, add another project to your solution, but this time a Game Library project. This is used to hold the data types that both your game and the content pipeline extension will need.

Set up your project references correctly as well. Add a reference to the game library project you just created to both the content pipeline extension project as well as the game project.Add a reference to the content pipeline extension project to your game’s content project as in the previous example.

Now, let’s create the shared data type that will represent your game’s level. In your game library project, replace the class and namespace that it auto-generated with the following:

tmp14-95_thumb


 

 

 

 

Debugging a content pipeline extension

Figure 9.7 Debugging a content pipeline extension

You don’t need anything fancy for this example. Simply hold a couple lists of Vector3, one of which holds the position of each model in your world, and the other holds the rotation portion. Before the content pipeline extension portion, let’s implement the game portion. Add the following variables to your game project:

tmp14-97_thumb

Later in the topic, you learn how to include things such as Model objects in your runtime classes, but for now add a model to your content project.The downloadable example uses the depthmodel.fbx model. In your LoadContent method, create the following objects:

tmp14-98_thumb

Finally, replace your Draw method with the following:

tmp14-99_thumb

 

 

 

tmp14-100_thumb

Here, you use the data from your level to render the model wherever it says after enabling the default lighting model.With that out of the way though, you are almost ready to create your content pipeline extension. First, add a new file to your content project called mylevel.mylevel, and include the following data in it:

tmp14-101_thumb

This is the data you are going to import that represents your level. Each line is six different numeric values. The first three represents the position in the world, and the next three represents the yaw, pitch, and roll angles of rotation for the model. Now, go to your content pipeline extension project and remove the default ContentProcessor class it auto-generated, and replace it with the following:

tmp14-102_thumb

Much like your processor, use an attribute to describe your importer. The first parameter is the extension of the file the class will import; in this case, we picked .mylevel.You also need to derive from the ContentImporter class using the type you will fill with the imported data.This object has a single abstract method (again much like the processor) called Import.You can use this implementation of the method:

tmp14-103_thumb

 

 

tmp14-104_thumb

Note

If you have compilation errors with the StreamReader class, add a using clause at the top of the code file for System.IO.

Notice that the method is quite simple.You create your level object that you’ll return after importing the data.You create the two lists that will store the data.You then use the standard runtimes StreamReader class to read through the file (which is passed in as a parameter to the import method).You split each line of the file into the individual numbers they contain and drop those into your two lists, and you are done.

Now, this code doesn’t check errors, so if something goes wrong, it won’t work.We discuss ways to handle this in a few moments. Build the content pipeline extension project, go back to your content project, and notice that your mylevel.mylevel file uses the My Level Importer importer. If it is not, select the content importer, and make sure that the Build Action is set to Compile. If you run your application now, you see your level in action, as in Figure 9.8.

So as you see, this works, but who would want to build a level that way? You still have to create a model in your game, and you use only a single model in the level. It would be better to make your importer work, and import models and textures and everything all in one go to make a much better level.

Next post:

Previous post: