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.

Possible Duplicate:
Role of an entity state in a component based system?

So, I've heard a bit about a model of game programming that involves creating entities and then attaching different components to them to change their behaviour. So, an enemy entity might have attached components of AI, collision physics, etc. This seems like an excellent way to solve a lot of the problems in creating an engine, but I don't really have a precise idea of what exactly an entity or component is.

So my question then: what exactly should an entity object encapsulate? How would I go about attaching components to it?

share|improve this question
    
Have you searched this site? There are lots of questions about entities and components already, and I expect they will provide the answers you need. –  Kylotan Feb 25 '12 at 14:15
add comment

marked as duplicate by Tetrad Feb 25 '12 at 17:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

The whole point of a component-based entity system is that it's flexible. Each entity can have exactly and only what it needs. If a particular entity needs a collision volume and a place in the world, then it has that. If a particular entity doesn't need an AI, then it doesn't have that.

As to how you go about attaching them, there are two ways to do it: completely free-form, and slot-based.

By slot-based, I mean that there is a set of pre-defined components that every entity could have. There are a set of specific classes that is hard-coded into the entity class. You check at runtime as to whether the entity actually has those components or not.

A more free-form system allows you to put any number of components into the system. That is, the set of possible components is not hard-coded into the entity. Components in such a system would be named. To access a component of an entity, you would call a particular function and give it a name. It would search its list of components and return that component.

In the latter case, your components would need either polymorphism or some form of type-erasure or something in order to store them in a homogenous list and fetch them components later.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.