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've been fooling around with XNA today, attempting to develop a pure-component model game engine.

I've been working on a GraphicsSubsystem class, and realized that it will need both an Update method (to update the individual graphics components, e.g. animated sprites), and a Draw method (it'll be the only Subsystem with a draw method).

My original plan had been to simply add a "GraphicsSub.Draw()" line in the main game's Draw method, and a "GraphicsSub.Update()" line in the main game's Update method. Totally valid, works fine.

However, I've discovered that XNA has two interfaces, "IUpdateable" and "IDrawable" that seem like they might be designed for exactly that, and that they'd provide a better framework for achieving this functionality compared to my simple method calls.

When I implement IUpdateable or IDrawable, what am I telling the XNA.Game to actually do?

Is it as simple as being included in the base.update() and base.draw() during the main game's Draw() and Update() calls?

share|improve this question
    
Note that "simple method calls" have the substantial benefit of being simple ;) –  Andrew Russell Dec 12 '11 at 1:29
    
I agree too. The only place at the moment where I happen to use a GameComponent is in my FPS Counter/Displayer class which is completely generic and "plug and play". There's nothing wrong with just calling Draw() and Update() manually, and that's what I do for all my other subsystems. –  David Gouveia Dec 12 '11 at 1:33
2  
Then again, there's nothing wrong at all about using it, as long as you're using it at the right abstraction level. Use it for your main game subsystems such as graphics, audio, physics, state management. Do not use it on individual game objects or similar things. And I've seen people do that before... –  David Gouveia Dec 12 '11 at 2:07

1 Answer 1

up vote 6 down vote accepted

TL;DR

Just implementing IUpdateable or IDrawable will do nothing.

Instead have GraphicsSubsystem inherit from the DrawableGameComponent class, and override the virtual Update and Draw methods. Then all you have to do is create your GraphicsSubsystem object and register it using Components.Add(objectName) on the Game class.

If you wanted to do the same thing using those interfaces, you'd actually need to implement IGameComponent, IUpdateable and IDrawable, which is exactly what DrawableGameComponent already does. And of course you'd still need to register the class on Components for it to do anything.

Long Answer

Just implementing an interface does nothing other than guarantee your class will have a certain set of methods/properties/events (and be compatible with anything that expects that interface).

XNA has a components system where you register a component at the start of your game, and the Initialize/Update/Draw methods will be automatically called back for you, along with a series of other functionalities (such as controlling the update order or hiding components).

This system is related to three basic interfaces that you should know: IGameComponent, IDrawable, IUpdateable.

You can create your own class and implement from any/all of these interfaces and this is what they do basically:

  • IGameComponent - Has an Initialize method and has the added catch that you can only add a class to Components if it implements this interface.
  • IUpdateable - Has an Update method. If an IGameComponent also implements this method, then Update will be called for you automatically.
  • IDrawable - Same as IUpdateable but for the Draw method.

But since it would be troublesome to implement these interfaces everytime, XNA already provides two implementations which you can just inherrit from and they're ready to go. They are:

  • GameComponent - This one implements both IGameComponent and IUpdateable but not IDrawable. Use this for components that have no visual representation.
  • DrawableGameComponent - Inherits from GameComponent and implements IDrawable on top of it. Use this for components that have a visual representation.
share|improve this answer

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.