Thursday, November 26, 2015

xna - How to add several GameComponents of the same type?


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.



Answer




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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...