I am trying to get a content type reader for my class working, but it doesn’t seem to be doing anything. Is there anything I need to do to get this to work?
public class Map
{
...
/// <summary>
/// Read a Map object from the content pipeline.
/// </summary>
public class MapReader : ContentTypeReader<Map>
{
protected override Map Read(ContentReader input, Map existingInstance)
{
Map map = existingInstance;
if (map == null)
{
map = new Map();
}
map.TileSetFile = input.ReadString();
map.Dimensions = input.ReadObject<Point>();
map.Tiles = input.ReadObject<int[]>();
map.Load(input.ContentManager);
return map;
}
}
}
The xml is read in properly, but the Read function seems to not be called. Any ideas?
Do I need more than just what I have here.
Disclaimer: I am by no means an expert on the XNA Content Pipeline
Before giving a short summary on the content pipeline and how to extend it, i’d like to mention that since XNA 3.1, implementing a ContentTypeReader and ContentTypeWriter is no longer necessary.
In XNA 3.1, Automatic XNB serialization was introduced (by using Reflection), and so if you do not include any writer and reader, XNA will take care of that for you (i believe excluding some cases).
Read more here: Automatic XNB Serialization
The XNA Content Pipeline is a process that is made of the following steps:
Most of these occur at design time (when you are using Visual Studio to add art assets to your game and when you’re building your game). The last part is executed at runtime, when you run your game and call Content.Load to load your assets.
The XNA Content Pipeline, being open for extension, allows you to support more than the built in content types that are shipped with the XNA Framework, and so allows you to introduce new content types and new file formats to be processed as input for your game.
In the following diagram, you can see the chain of events that are executing when extending the content pipeline:
In order to create an extension to support a new type (Map in your example), you must do the following (some of which i suppose you already did):
Your ContentTypeWriter class should implement this method:
Note that in this example, i’ve used some other CharacterReader class, you will have to register your reader type with it’s full name and containing assembly.
Read about the GetRuntimeReader method here: ContentTypeWriter.GetRuntimeReader
I havev created a demo project with a new type called Character, an importer, processor and reader/writer for it.
You can get it here: Sample project
*note that you can put this line in your processor/importer in order to debug and see that visual studio is actually triggering the code for your Content Pipeline extension library.
*Another thing to note is that your new type (Map, Character, whatever) should be defined in its own shared assembly and planned correctly, since this type should be used both in your actual Game and in your Content Pipeline extension library.
Hope this helps.