Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to understand how to use the XNA XML content importer to instantiate non-trivial objects that are more than a collection of basic properties (e.g., a class that inherits from DrawableGameObject or GameObject and requires other things to be passed into its constructor).

Is it possible to pass existing external instances (e.g., an instance of the current Game) to the constructor of an object that's being created using the default XNA XML content loader?

For example, imagine that I have the following class, inheriting from DrawableGameComponent:

public class Character : DrawableGameComponent
{
  public string Name { get; set; }

  public Character(Game game) : base(game) { }

  public override void Update(GameTime gameTime) { }
  public override void Draw(GameTime gameTime) { }
}

If I had a simple class that did not need other parameters in its constructor (i.e., the Game instance), then I could simply use this XML:

<XnaContent>
  <Asset Type="MyNamespace.Character">
    <Name>John Doe</Name>
  </Asset>
</XnaContent>

...and then create an instance of Character using this code:

var character = Content.Load<Character>("MyXmlAssetName");

But that won't work because I need to pass the need to pass the Game into the constructor.

What's the best way to handle this situation? Is there a way to pass in things like the current Game using the default XNA XML content loader? Do I need to write my own XML loader? (If so, how?) Is there a better object-oriented design that I should be using for my classes?

Note: Although I used Game in this example, I'm really just asking how to pass any type of existing instance to my constructors. (For example, I'm using the Farseer Physics Engine, and some of my classes also need a reference to the Farseer World object too.)

Thanks in advance.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

You want to load only the information about the character from the XML, and use that information in your character class:

public class Character : DrawableGameComponent
{
    public CharacterInfo CharacterInfo { get; private set; }

    public Character(Game game, string characterAssetName) : base(game)
    {
        _assetName = characterAssetName;
    }

    protected override void LoadContent()
    {
        CharacterInfo = Game.Content.Load<CharacterInfo>(_assetName);
    }

    public override void Update(GameTime gameTime) { }
    public override void Draw(GameTime gameTime) { }
}

//In your data-types project:
public class CharacterInfo
{
    public string Name { get; set; }
}

See this and then this blog post for more details.

share|improve this answer
    
This is the solution I've taken in my projects, too. –  Alex Schearer Jun 29 '11 at 23:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.