I'm writing a small 2D shooter game in XNA, and I've decided that, so that one could implement custom made content in the game, it loads definitions of the game objects from XML files. When I found out that XNA has an easy to use XML serializer, it made it extra easy. The problem is, my objects that I want to serialize are DrawableGameComponent
s. XNA's XML serializer, the ContentTypeWriter
class which you extend to create custom content writers, requires that the object have a constructor with no arguments to default to. DrawableGameComponent
, however, requires a Game
object in its constructor and will not let you set the game after the object is initialized. I cannot modify the behavior of the ContentTypeWriter
enough, however, to accept a non-blank constructor, because the content is loaded by an entirely different method that I cannot overwrite. So essentially I have this:
class Star : DrawableGameComponent{
public Star(Game game)
: base(game)
{
}
}
With the ContentTypeWriter
requiring a constructor with no arguments. I can't create one though, because then I have no way to get a Game object into the Star
class. I could just not make it a DrawableGameComponent
, but I am trying to decouple these objects from the primary game class such that I can reuse them, etc. and without a Game
object this is absurdly difficult. My question therefore is, does anyone know how to modify ContentTypeWriter enough to allow a constructor with arguments, or any ways around this?
I also thought about writing my own XML parsing code using XPath or the Linq XML classes but XNA throws a fit if I have any XML files in the project that do not follow the XNA schema and won't build. Would it be reasonable to Write a base class with only the primary fields of the class and a DrawableGameComponent
version that uses the decorator pattern, and serialize only the base? I'm pulling out my hair trying to get around this, and wondering what exactly I should be doing in this situation.