4 min read

Extending the Content Pipeline - Part 8

Using the Extension in your Project

We are almost done! We just need to use our extension in our game. But first, we need a level file for it to read. Feel free to create your own (look at the format that we followed at the beginning of Part 5. Or, alternately, you could download this file:

level1.level

You can also copy this file and edit it in any text editor to make other levels.

Once you have a level file, add it to your content directory like we have done before. At this point, you can check to make sure that your projects are linked together correctly, and that everything is working. When the .level file is added, it automatically searches for the importer and processor to use (which we specified when we made our importer and processor). If everything is working correctly, the project will automatically set up your level file to use your new importer and processor. You can check this by right-clicking on the level file and choosing Properties from the popup menu. A new window will appear (if it isn’t already visible) that looks like the image below:

Screenshot 7

For the content importer and the content processor, it should say “Level Importer” and “Level Processor” respectively. If it doesn’t say those, then check the list of importers and processors available (the little dropdown list in the properties window) and see if you can find it. If you can find them, then just select them and you should be OK. If they aren’t there at all, then you’ve made a mistake somewhere along the line–probably with the project linking in [Part 2](https://rbwhitaker.com/tutorials/xna/content-pipeline/extending/part-2/.

Additionally, in the level file’s properties, you’ll probably also want to make sure that under Build Action it says Compile.

Hopefully, though, your level file has been set up with the right importer and processor, and you are now ready to use it. Go to your main game class (Game1.cs), find the LoadContent() method, and add the following line:

LevelLibrary.Level level = Content.Load<LevelLibrary.Level>("level1");

This is all we need to do! Our level is loaded. However, let’s print out some stuff about the level, just to make sure that it actually got loaded correctly. (I can’t count the number of times that I’ve gotten to this point, and instead of useful level information, I just got a big pile of 0’s or something like that.) I’ve made my program a console application so that I can print to a console, and added the following line of code right below the previous line:

for (int row = 0; row < level.Rows; row++)
{
    for (int column = 0; column < level.Columns; column++)
    {
        Console.Write(level.GetValue(row, column));
    }
    Console.WriteLine();
}

The final code for the entire class should look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ContentPipelineExtension
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        LevelLibrary.Level level;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            level = Content.Load<LevelLibrary.Level>("level1");

            for (int row = 0; row < level.Rows; row++)
            {
                for (int column = 0; column < level.Columns; column++)
                {
                    Console.Write(level.GetValue(row, column));
                }
                Console.WriteLine();
            }

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

Now when I run the program, I get the following output:

Screenshot 8

It looks like it worked!

If you are still having trouble, you might want to compare it to the complete project. The complete project can be found back on the first page of this tutorial. I realize this has been a fairly long tutorial, but hopefully, it helps!