# In a component/entity system, what's the correct way to implement entity-specific components?

I'm trying to create a basic C/E game engine for Android, and I'm trying to re-create pong as a basic warm-up. I currently have a EntityManager class that contains a list of all the UUIDs for its Entities, and has a render() function that calls render() on all Components that implement RenderableComponent (a basic interface with a render() function itself), and the same for update().

Stop me if I'm already doing something incorrectly...

My question is, should I be creating specific components that only apply to each entity? (E.g. a BallMovement, BallDraw, etc. component for the ball) or what is the correct way to create components for Entities?

No, I think your components should be as generic and reusable as possible. And the examples you gave certainly don't need specific components. Here's an example of the type of components you could be thinking about:

• TransformComponent - Has as Position in the world.
• SpriteComponent - Has a Sprite and renders it at TransformComponent.Position
• MovableComponent - Has a Velocity and updates TransformComponent.Position
• BoxComponent - Has a Rectangle and detects collisions with other BoxComponents

Then, all of these have parameters that will vary from entity to entity.

For example, and still thinking about the ball:

• The TransformComponent could store the ball's initial position
• The SpriteComponent could keep a reference to the ball sprite
• The MovableComponent could store the ball's initial velocity.
• The BoxComponent could store the ball size and have a special handler for its collision event

Then your paddles might also have some of these components, but they will probably have different initial parameters for each of them, and this is what will make them unique and different from the ball entity. A good component for them might be an InputComponent that handles user input and updates the TransformComponent when the player presses the up or down keys.

PS: If you have never tried Unity3D, I think it's a good place to start wrapping your head around these concepts!

• Question: OP says he is rendering on a component-to-component basis (I personally use "systems" to handle rendering but I'm not saying which approach is better). Your example mentions SpriteComponent using TransformComponent.Position. How would you suggest OP handle the coupling between them? – Mike Cluck Mar 3 '12 at 2:41
• @MikeC Actually I haven't researched much into the best way to implement a component based system, so this might not be the best way, but following my gut instinct I would it implement it like this: Any component that is registered with an entity would automatically store an internal reference to that entity. And all entities would have a GetComponent(Type) method. Then on initialization, the SpriteComponent could simply call entity.GetComponent(tyepof(TransformComponent)) and store it locally. Then use that reference when rendering. – David Gouveia Mar 3 '12 at 2:55
• @MikeC Another possibility that I can think of is a message based system, where any component can send a message through the entity to every other component so that they can react accordingly. In that scenario, the TransformComponent might know how to react to a GetPosition message by returning its internal position, and the SpriteComponent would simply send a message of that type through the entity when it required the position. – David Gouveia Mar 3 '12 at 3:00
• @DavidGouveia I can't say I've researched it much myself, kind of learning as I go, but personally I'd feel uneasy creating direct coupling between components like that. I'm actually in the process of trying to resolve this issue in my own engine, which is why I asked. – Mike Cluck Mar 3 '12 at 4:23
• @DavidGouveia Messages aren't enough. They are good for notifying components of things that happen rarely, but components also need to access information on-demand, like the position. I've posted an answer here about how I implemented my CBS: gamedev.stackexchange.com/a/23759/6188 – Paul Manta Mar 3 '12 at 13:35