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.

So after reading about components for a bit I want to make a very simple spaceshooter game that uses components and systems. However, I'm a bit confused on how much scope each component should have. For example, let's say the game had asteroids that fly around and if you hit them, you take damage. They also have health. The asteroid entity may then contain these components:

  • SpriteComponent (Texture2D texture)
  • HealthComponent (int amount)
  • CollidableComponent (bool rigid)
  • MovementComponent (int x, int y)
  • RotationComponent (int degrees)

Now let's say we had a RenderSystem. Its Update() method would require SpriteComponent, MovementComponent, and RotationComponent to draw an entity.

That's all fine and good but when would have MovementComponent and not RotationComponent? Couldn't we just have a TransformComponent that contains the x, y, and rotation? It feels a little weird designing these components to only be a container of one data-type. But then where do you draw the line? All sprites also have a position and rotation amount, so should the SpriteComponent contain the TransformComponent, or should the members of TransformComponent be moved to SpriteComponent?

But then the problem is if you wanted an entity with a position and rotation but no sprite. Then you either have to give it an empty texture (not necessarily a bad solution) or maintain TransformComponent and just make SpriteComponent contain it as well (or just pass both).

There's a lot of decisions to be made and I really feel similar to when I began object-oriented programming. "What should be objects? What should its methods be? Should this be public or private?" Obviously I've figured all that out now but this new component-based paradigm is giving me these same problems now. Does anyone have any advice or standards for component-based design for typically used components and how one normally would set something like this up?

share|improve this question
    
I've found this article to be helpful: piemaster.net/2011/07/entity-component-artemis –  michael.bartnett Oct 26 '11 at 17:34
    
There are so many questions on these boards on components. Please, search on the components-based tag. –  Nick Wiggill Oct 26 '11 at 19:32
add comment

3 Answers

up vote 2 down vote accepted

Couldn't we just have a TransformComponent that contains the x, y, and rotation?

Yes. This is exactly what the Unity Transform component does.

It feels a little weird designing these components to only be a container of one data-type. But then where do you draw the line?

You don't have one data type. You have one responsibility.

so should the SpriteComponent contain the TransformComponent, or should the members of TransformComponent be moved to SpriteComponent?

The way Unity does it, is that all components are contained by the GameObject itself. Components don't "contain" other components, but they can have references to them.

All sprites also have a position and rotation amount..

There are two things Unity does to handle this.

1) All game objects innately have a Transform component.

2) When authoring game objects, you can specify that certain components require the game object they're attached to to have other components. They do this through their RequireComponent attribute. You can do things like say "when this component starts up, and the game object it's attached to doesn't have my required components, throw an error".

share|improve this answer
    
Say "Unity" one more time... but true, I can think of much worse examples. –  Nick Wiggill Oct 26 '11 at 19:31
add comment

I would suggest to read the following post from StackOverflow (if you already haven't), because it has lots of information and references to Components documentation.

Component Based Game Engine Design

As previously said, entities (or GameObjects) contain components. And components do not contain other components but references to them. I hope this helps! ;)

share|improve this answer
add comment

That's all fine and good but when would have MovementComponent and not RotationComponent? Couldn't we just have a TransformComponent that contains the x, y, and rotation?

There is no right answer to this question as it depends on the level of granularity you choose for a specific project. TransformComponent is very typical as far as I can judge. However for your test project you might want to have a PositionComponent for in-game GUI.

All sprites also have a position and rotation amount, so should the SpriteComponent contain the TransformComponent, or should the members of TransformComponent be moved to SpriteComponent?

Neither. In first case some components will become semi-entities which is an unnecessary over-complication. In second case you will have to add similar Transform into elements that need spatial info but are not visible (e.g. some AI or physics related stuff).

share|improve this answer
add comment

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.