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.

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.

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
Do you think it somehow affects performance if I continue using that particular class? –  bodycountPP Feb 1 '12 at 14:37
    
@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. –  David Gouveia Feb 1 '12 at 14:42
1  
@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. –  David Gouveia Feb 1 '12 at 15:04
    
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? –  bodycountPP Feb 1 '12 at 19:39
1  
@bodycountPP Yes it's perfectly fine to make your BasicModel class require an instance of GraphicsDevice on its constructor. –  David Gouveia Feb 2 '12 at 4:16
add comment

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.