Building a Content Pipeline Extension - Part 6/8

Parts > 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8

Building the Type Writer

Remember that in the content pipeline, after an object has been processed, we need to write it out to a .xnb file. Technically, this is the last stage of the content pipeline, since the fourth component is actually part of the game itself. The content type writer is the thing that writes this out to a file, and this is what we will be making next.

Once again, there is a template in XNA for content-type writers that we will use. So right-click on the content pipeline extension project ("LevelContentPipelineExtension" is what mine is called) and choose Add > New Item. In the Add New Item dialog that appears, choose the Content Type Writer template and give the writer a name. I've called mine "LevelWriter.cs".

Near the top of the file, find the line that says:

using TWrite = System.String;

and change it to:

using TWrite = LevelLibrary.Level;

This tells the content pipeline what type of object you are going to write to an XNA file.

In the Write() method, replace all of the code with the following:

            output.Write(value.Rows);
            output.Write(value.Columns);
            for (int row = 0; row < value.Rows; row++)
            {
                for (int column = 0; column < value.Columns; column++)
                {
                    output.Write(value.GetValue(row, column));
                }
            }

This writes out all of the important stuff to the file.

The last thing we will need to do is specify the file reader that will be used when we load it in the game. This is done in the method at the bottom of the class. Replace the stuff inside the GetRuntimeReader() method with this:

return "LevelLibrary.LevelReader, LevelLibrary";

This is the full namespace path and class name of the reader (which we will create in the next step), along with the assembly name which is the project name by default.

This should complete the type writer. The full code is shown below:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
 
using TWrite = LevelLibrary.Level;
 
namespace LevelContentPipelineExtension
{
    [ContentTypeWriter]
    public class LevelWriter : ContentTypeWriter<TWrite>
    {
        protected override void Write(ContentWriter output, TWrite value)
        {
            output.Write(value.Rows);
            output.Write(value.Columns);
            for (int row = 0; row < value.Rows; row++)
            {
                for (int column = 0; column < value.Columns; column++)
                {
                    output.Write(value.GetValue(row, column));
                }
            }
        }
 
        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "LevelLibrary.LevelReader, LevelLibrary";
        }
    }
}

Continue on to part 7

Parts > 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8


Troubleshooting.png Having problems with this tutorial? Try the troubleshooting page!