Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a small demo and I want to create a class that draws messages in screen like fps rate. I am reading a XNA book and they comment about GameComponents. I have created a class that inherits DrawableGameComponent

public class ScreenMessagesComponent : Microsoft.Xna.Framework.DrawableGameComponent

I override some methods like Initialize or LoadContent. But when I want to override draw I have a problem, I would like to pass some parameters to it.

Overrided method does not allow me to pass parameters.

        public override void Draw(GameTime gameTime)
            StringBuilder buffer = new StringBuilder();

            buffer.AppendFormat("FPS: {0}\n", framesPerSecond); // Where get framesPerSecond from???

            spriteBatch.DrawString(spriteFont, buffer.ToString(), fontPos, Color.Yellow);

If I create a method with parameters, then I cannot override it and will not be automatically called:

        public void Draw(SpriteBatch spriteBatch, int framesPerSecond)
            StringBuilder buffer = new StringBuilder();

            buffer.AppendFormat("FPS: {0}\n", framesPerSecond);     
            spriteBatch.DrawString(spriteFont, buffer.ToString(), fontPos, Color.Yellow);

So my questions are:

  • Is there a mechanism to pass parameter to a drawableGameComponent? What is the best practice?
  • In general is a good practice to use GameComponents?
share|improve this question
Why no place the logic for calculating the FPS counter in the update method of this class? – Luis Estrada Oct 16 '12 at 1:16
I think that fps is just an example of the data he woul like to pass to the component. – Ken Oct 16 '12 at 6:56
Yes, fps is just an example. I plan to pass more parameters, as many as I need. – Oscar Foley Oct 16 '12 at 8:37
up vote 3 down vote accepted

Give your DrawableGameComponent some properties e.g. framesPerSecond, which you set before calling Draw(GameTime). This might only be appropriate if the property is a 'natural' part of the Component.

The other alternative is to use the game service mechanism. Game services are a mechanism for maintaining loose coupling between objects that need to interact with each other. Add a service class to the game service container;

  //this is usually in the Game.Initialize()
  Services.AddService(typeof(ContentManager), Content);
  Services.AddService(typeof(Camera), camera);

The DrawableGameCompontent object can then access that service object through the service provider;

//DrawableGameComponent method
public void Draw(GameTime gt)
        Camera camera = (Camera)Game.Services.GetService(typeof(Camera));
        foreach (ModelMesh mesh in invaderModel.Meshes)
            foreach (BasicEffect effect in mesh.Effects)
                //Effect Settings Goes here
                effect.LightingEnabled = true;
                effect.World = world;
                effect.Projection = camera.Proj;
                effect.View = camera.View;


This question has an interesting discussion on the pros and cons of using GameComponents.

share|improve this answer
Thanks. It worked. In fact all the answer were pretty accurate but could choose only one as the accepted answer :) – Oscar Foley Oct 21 '12 at 10:32

There are mainly two ways:

  1. As Luis Estrada says, add logic to count Update- and Draw-Calls (this numbers might differ) to the component. If no other component needs this properties, this might be the best way.
  2. If other components also need to know about the current frame-rate, it might be a good way to implement a GameService that is responsible for this. In each DrawCall to your ScreenMessagesComponent access the Service and draw the values to the screen.

Have a look at this for calculating frame-rates: Calculating frames per second in a game

share|improve this answer

I have answered a very similar question to this over on Stack Overflow. To summarise:

The best way to pass an argument into a game component's method is to pass an argument in! Unfortunately this precludes the use of GameComponent - because its interface is fixed. It's a fairly weak API for this reason.

It's best to treat GameComponent as an example of how you can set up a "game component" or "actor" system in your game. Like many other parts of XNA (sprite fonts, models, the Game class itself, etc) - you're able to and encouraged to replace it if you need functionality that it does not provide.

Although it sounds like you don't really need an actor or component system here at all. I imagine each "component" you have will need different data passed in? Simply create a class with a Draw method? Take a look at this answer.

But, if you must use GameComponent, then perhaps the next-best method is to set member properties or fields on the GameComponent before their Draw and/or Update methods get called (they get called in Game.Draw and Game.Update - so where you call base.Draw and base.Update in your own game class).

(You could use another method, though. Just do the simplest thing that could possibly work.)

share|improve this answer

The problem is that something like GameComponent has a fixed interface; you can't pass it in as an argument. Thus, what you can do is create a new item that acts as a Service in your main game class. Then, just add it to your game's services and try to get it when you need to draw it.

So, at the start of your game (constructor or Initialize method, whatever works) you can do:

DebugInformer informer = new DebugInformer();
Services.AddService(typeof(DebugInformer), informer);

DebugInformer is a class of your own design. Give it properties that work for you like FramesPerSecond. You can add other things you want that are specific to your game, such as ItemsOnGround and whatever else suits your fancy. Later in the code, you can get the properties out and use them like so:

//In the middle of your specific Component's draw call:
DebugInformer informer = Game.Services.GetService(typeof(DebugInformer)) as DebugInformer;
if (informer != null) {
    // Informer is present, use informer.FramesPerSecond and other stuff

Services were designed just for problems like this, so don't feel afraid to use it.

Good luck!

share|improve this answer

Your Answer


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.