Component based >? Inheritance based design

Started by
26 comments, last by Jabzor 12 years ago
You can read about components in the chapter 14 of the Jason Gregory's book Game Engine Architecture (third time in two weeks that I recommend this book, I am not Jason tongue.png)

Component based model could be complemented with data oriented design: http://research.scee.net/files/presentations/gcapaustralia09/Pitfalls_of_Object_Oriented_Programming_GCAP_09.pdf

[size=1]Project page: [size=1]<

[size=1] XNA FINAL Engine[size=1] [size=1]>
Advertisement
Thanks, I have actually just read this a few days ago, someone recommended it (DOD) to me as a better alternative to OOP design in game development. Definitely interesing, but I am starting to feel I have too much on my plate. smile.png

Could anyone please answer my previous question? I'd really appreciate it.
Another one from DICE: http://publications.dice.se/attachments/AStepTowardsDataOrientation.ppt

And another good one:
http://www.google.com/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=1&ved=0CCYQFjAA&url=http%3A%2F%2Fwww.gdcvault.com%2Fplay%2F1911%2FTheory-and-Practice-of-the&ei=QqiIT6uIFZKy8ATCsenhCQ&usg=AFQjCNGVTFg3QbWsg62Hqimepl-bQB1LaQ

[size=1]Project page: [size=1]<

[size=1] XNA FINAL Engine[size=1] [size=1]>

In my inheritance based structure, I could have my ship entity that owns 2 wing entities that move and emit particles based on the ships velocity, and can own weapon entities, that are also animated, and can use auto targeting (turn toward and target the closest enemy).

With component based design how is this implemented? Should the ship, wings, weapons be separate entities (each with its own set of components) that are tied together somehow, or I should just have my ship entity and the wings, ship weapons, wing weapons be components?


They are different entities with different components. But the entities are in a hierarchy (not inherence).
You should have something like this:
Ship = new GameObject();
// Fill Components
Wings = new GameObject();
// Fill Components
Wings.Father = Ship;

Then, when you move the ship the wings will move equally.

[size=1]Project page: [size=1]<

[size=1] XNA FINAL Engine[size=1] [size=1]>
Thanks!

In this kind of hierarchy, should the parent and child know about each other's components in general, and work accordingly?

I am just having trouble thinking out a general design that obeys the most important concepts of component based design:
- Ideally all entities should be treated exactly the same (on the outside nothing knows or can know what components does the entity have)
- Each component is capable of working completely independently without the assumption or knowledge of any other component within the same entity
- Each component should still somehow be able to communicate with one another (or provide information that another entity if exists might make advantage of)

For the communication, the way I picture it huge switch-case blocks will need to be used for the "just in case there is a xyz component in this entity". Is that how it should be?

For the custom hierachy, when an entity is in Father-Child relationship, how does that affect the general operation? Does each component have to have a different set of switch-case statements in case the holding entity is a Child, or is a Father?

Or should I use more specific single-purpose components, so I could use one that is making the assumption that the entity in which it will be used is a child or father to something? Wouldn't that however beat the purpose of the whole concept of nothing makes any assumptions?

Thanks!

In this kind of hierarchy, should the parent and child knows about each other's components in general, and work accordingly?


Depends how you interpreted “knowing each other”. The child only need to know the father world matrix and nothing more. It is possible that some custom script could also read some information from the father.


- Ideally all entities should be treated exactly the same (on the outside nothing knows or can know what components does the entity have)



Yes. The entity does not have an update, only the components.


- Each component is capable of working completely independently without the assumption or knowledge of any other component within the same entity


This is tricky. For example, a renderer component needs the world matrix from the transform component and the model from the model component. You just can’t avoid not knowing anything.
That said, yes, the component update has to be isolated. The only exception is the transform component; a physic component for instance could modify the entity’s transformation.


For the communication, the way I picture it huge switch-case blocks will need to be used for the "just in case there is a xyz component in this entity". Is that how it should be?


No, messages or events are the best. The component that needs information asks for the information, not the other way around. Read Jason book, read Unity3D reference, or look at my engine.


For the custom hierachy, when an entity is in Father-Child relationship, how does that affect the general operation? Does each component have to have a different set of switch-case statements in case the holding entity is a Child, or is a Father?


The components normally are very simple. Transformation components (a maybe physics components, I don’t know) are the only that needs to know the entity father. And actually, in the actual implementation the transform component is the only that holds the father (not even the entity). This is intended for data oriented optimizations and because is somehow logical.

[size=1]Project page: [size=1]<

[size=1] XNA FINAL Engine[size=1] [size=1]>
Please, press F5. I edited the text.

[size=1]Project page: [size=1]<

[size=1] XNA FINAL Engine[size=1] [size=1]>
Thank you very much for your detailed answer and explanations. I think I have enough information now to start redesigning what I have, and when specific issues would arise I would come back for help.

I'll also read through the relevant part of the book you suggested, and look through your engine and Unity 3D. I'll also keep DOD in mind, but I'm probably just going to focus on one thing at a time (transitioning and adapting to composition).
Thanks for all the links, I've found this thread pretty useful. The way I use game states now seems to be vastly different to when I had an inheritance model. Because with that model it would be that each state updates and draws everything relevant to that state. But with the component system, it seems that the 'component managers' will just update every component every loop (independent of the game state), removing the need for separate 'event', 'update' and 'draw' virtual functions per state. It seems the only use for states now is just for loading and unloading game elements into the 'entity manager' every new state. I'm guessing each state should have it's own entity managers and component managers? Does this sounds like the best approach? Thanks.
What do you call game states?

[size=1]Project page: [size=1]<

[size=1] XNA FINAL Engine[size=1] [size=1]>

This topic is closed to new replies.

Advertisement