2
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ I've found this article to be helpful: piemaster.net/2011/07/entity-component-artemis \$\endgroup\$ Commented Oct 26, 2011 at 17:34
  • \$\begingroup\$ There are so many questions on these boards on components. Please, search on the components-based tag. \$\endgroup\$
    – Engineer
    Commented Oct 26, 2011 at 19:32

3 Answers 3

2
\$\begingroup\$

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".

\$\endgroup\$
1
  • \$\begingroup\$ Say "Unity" one more time... but true, I can think of much worse examples. \$\endgroup\$
    – Engineer
    Commented Oct 26, 2011 at 19:31
0
\$\begingroup\$

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! ;)

\$\endgroup\$
0
\$\begingroup\$

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).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .