Which is the way to pass parameters in a drawableGameComponent in XNA 4.0?

Posted by cad on Game Development See other posts from Game Development or by cad
Published on 2012-10-15T21:45:08Z Indexed on 2012/10/15 21:50 UTC
Read the original article Hit count: 166

Filed under:
|
|

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);
            base.Draw(gameTime);
        }

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);
            base.Draw(gameTime);
        }

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?

© Game Development or respective owner

Related posts about XNA

Related posts about xna-4.0