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.

We are just switching from a hierarchy based game engine to a component based game engine. My problem is that when I load a model which has has a hierarchy of meshes and the way I understand is that a entity in a component based system can not have multiple components of the same type, but I need a "meshComponent" for each mesh in a model. So how could I solve this problem.

On this side they implemented a Component based game engine: http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/

share|improve this question
    
I think this is too localized. –  jco Oct 16 '12 at 19:30
4  
I think its a general question. Can a game object have multiple instance of the same component? –  Mathias Hölzl Oct 16 '12 at 19:36
    
Yeah, it could've been, if it was asked like that. To me it seems as if he was looking for an answer to a very specific problem. –  jco Oct 16 '12 at 19:39
4  
"...entity in a component based system can not have multiple components of the same type..." - why not? –  Den Oct 16 '12 at 20:40
    
I don’t think it’s too localised. For instance in UE3, SkeletalMeshActor only has one SkeletalMeshComponent. It’s a common problem that can be addressed in many different ways. –  Sam Hocevar Dec 30 '12 at 20:53

4 Answers 4

up vote 12 down vote accepted

Your Position component could have a "parent/children" logic, where any Entity with a Position may have a parent and their position is relative to their parent. Instead of having several meshs on the same entity, you can make more than one entity, each with its own mesh and link them together. You can even make the children entities listen to their parent events (or whatever system you have for communication between entities) and react accordingly.

share|improve this answer
    
So I have a hierarchy of entitys and these entitys have componends and are linked togheter. Is it then still a Component based game engine =) –  Mathias Hölzl Oct 16 '12 at 19:44
    
@MathiasHölzl is that a question? This kind of hierarchy is not the same as the problematic hierarchy in OOP. This is just a graphical hierarchy, children Entities won't inherit functionality from their parent and give you trouble, this is usually done anyway (having a tree of stuff to render). You can also go with Asakeron answer to your problem, having a list of Meshs, I don't see how that's problematic. Maybe I don't understand your question? –  Luke B. Oct 16 '12 at 19:51
    
Ok, I think I got you. Thanks for helping –  Mathias Hölzl Oct 16 '12 at 19:59
    
-1 Not sure if that's really the way to go. If what you need is a component handling a hierarchy of meshes, why not having a ModelComponent that contains a hierarchy of meshes? Splitting an entity just for that sounds like the wrong solution to the problem. See Asakeron's and Byte56's answers. –  Laurent Couvidou Oct 17 '12 at 7:42
    
@LaurentCouvidou I don't see why using more than one Entity would be wrong, it seems like the a good solution for me. I just wanted to give a different alternative to having a list of meshs, though I agree that a list of meshs would also be a good solution. –  Luke B. Oct 17 '12 at 10:34

TLDR: By making the component consist of multiple meshes to begin with.

I agree with Asakeron/Byte56/Laurent in that another level of indirection is needed between the mesh/material pairs, and the entity itself. Instead of looking at the GraphicsComponent as vertices and materials, think of it as a blob of pixels on the final raster - how it/they get there is an implementation detail and nothing more.

I thought about this a lot for my project and I think the optimal solution is to make the GraphicsComponent a much higher level component, encompassing much of the functionality of the traditional 'Model' object - because this functionality isn't optional! To render those polygons a lot more than just the buffer data and the shader is needed, such as:

  • Position which you have mentioned
  • Skinning/Animation data
  • The Current Pass (e.g. if using two pass alpha)
  • Shadow casting information (if you are doing it)
  • Information on how and when to update the material
  • Culling functionality

And thats just for 3D assets, without considering particle systems, billboards, etc. But all of it is pertinent only to the graphics/rendering code - it doesn't affect the physics, sound or scripting, so it makes sense it should sit in the Graphics/Rendering component.

I ended up with:

Model : Entity, IHasGraphicsComponent, IHasSkeleton, IHasAnimationStore     //This is the 'game object' - it is passed to the GraphicsController
    ModelComponent : GraphicsComponent                      //This is the actual graphics component, used by the GraphicsController in the context of the game object.
        ModelComponentPart : GraphicsComponent              //This is also a graphics component
            Mesh                                        //These are implementation details
            Material
        ModelComponentPart : GraphicsComponent
            Mesh
            Material
    Skeleton
    Animations

In this:

  • Model is any game asset that has a graphics component.

  • The ModelComponent is analogous to the traditional Model and in fact, is, for 3D assets. The GraphicsComponent controller (if you use the Model-View-Controller pattern) is responsible for figuring out what type of graphics asset it is and drawing it correctly (note that ModelComponent is a subclass of GraphicsComponent).

There were also a couple of compromises in mine for simplicity and backwards compatabilitys sake, such as each GraphicsComponent is also an Entity, and Entity stores Position data directly so its only calculated in one place, but the idea is the same: GraphicsComponent handles what is needed to draw the item - all of what is needed - not just what comes from the modeller.

share|improve this answer

I would create my mesh component with a list of mesh objects. Each mesh object has the mesh data along with an offset. When drawing, the drawing system takes the position from the position component, then draws each mesh in the mesh component at position + offset.

You can have multiple meshes inside your mesh component, while still saying with a single mesh component per entity.

share|improve this answer

Your meshComponent may contain a list of meshes. I'm not sure how you are implementing your engine, but a system could easily iterate over all meshes and simply draw them.

share|improve this answer
1  
A mesh has also components like transform, physic, graphic... –  Mathias Hölzl Oct 16 '12 at 19:39
1  
So a mesh is an entity? Or is everything a component? The way I see it, transform, physic and graphic should be components in the entity not in the mesh, a mesh is just a description of vertices. –  Luke B. Oct 16 '12 at 19:43
1  
Yeah it should be a component but components can not have components so its hard to implement the hierarchy of the model. –  Mathias Hölzl Oct 16 '12 at 19:47
1  
I believe you should provide more information on how you intend to build your engine in order to get better answers. Entity Component Systems can be implemented in many ways, check this answer by Kylotan for more information on that. –  Asakeron Oct 16 '12 at 19:52

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.