1
\$\begingroup\$

in order to load 3D-meshes to my project i created a BasicModel class. I made it a DrawableGameComponent so I can override methods like Draw & LoadContent. Plus I have access to the GraphicsDevice etc.

Now I realized that if I try to add several instances of the same GameComponent I get an exception that says that it is not allowed to do this.

  1. Was it even a good thing to do, making the Class a GameComponent?

  2. Is there a way to somehow differ single Components, so that I can add several copies of it to the game?

Thanks in advance.

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Was it even a good thing to do, making the Class a GameComponent?

No. You should only consider using GameComponents for higher level concepts that sit directly on top of your Game class and will only require one instance to exist at any time.

Good examples are classes with Manager in their names such as a ScreenManager, InputManager or AudioManager, or classes that are built to be plug and play such as a FramerateCounter (see link) that you can easily reuse in any other project just by dropping it in.

So if for instance you created a scene graph class to store and manage the rendering and updating of your game world and all of its objects, you might consider making it a DrawableGameComponent. On the other hand, using it for each individual game object is not recommended at all.

Personally, I don't even use game components at all. I prefer having precise control over the updating and drawing of my classes, and if I need a reference to Game I can pass it on the constructor just as well without making the class a GameComponent. Don't let that discourage you from using them though, it's just a matter of personal preference, but just make sure you use them for the right task.

\$\endgroup\$
5
  • \$\begingroup\$ Do you think it somehow affects performance if I continue using that particular class? \$\endgroup\$ Commented Feb 1, 2012 at 14:37
  • \$\begingroup\$ @bodycountPP You wrote on your question that it doesn't let you add more than one GameComponent of the same type to the game component list (haven't verified this). How would you continue using it then? It's not even a matter of performance at this point - since it doesn't even work. My opinion is that you shouldn't even bother using them at all, and others seem to think the same. \$\endgroup\$ Commented Feb 1, 2012 at 14:42
  • 1
    \$\begingroup\$ @bodycountPP I've went ahead and checked it now, and indeed, the GameComponentCollection class doesn't let you add the same component multiple times. So, like I said, only use it for higher level concepts. \$\endgroup\$ Commented Feb 1, 2012 at 15:04
  • \$\begingroup\$ I am not really experienced but that would mean that I have to pass the graphicsdevice to every single instance of the BasicModel class. Is that a good programming style? \$\endgroup\$ Commented Feb 1, 2012 at 19:39
  • 1
    \$\begingroup\$ @bodycountPP Yes it's perfectly fine to make your BasicModel class require an instance of GraphicsDevice on its constructor. \$\endgroup\$ Commented Feb 2, 2012 at 4:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .