Metamist
Metamist

Reputation: 382

Entity Component System confusion

Now, before I start, I want to say that I am aware that there are no standard way to implement an ECS. However, there is one method that I am interested in, but confused about one particular thing.

Say you have an Entity with a TransformComponent (contains a position), and a RenderComponent (contains a Sprite), and along with this you have a RenderSystem separate from the Entity. The RenderSystem will handle the rendering of all components with a RenderComponent, and will not care what entity it is attached to. But since a Sprite requires a position, the RenderSystem will need the TransformComponent as well.

Now comes the part I am confused about, if all components are stored in their own vector of that component type, how will I get all entities that have both a TransformComponent as well as a RenderComponent? I do not want to use the naive method of looping through all entities in every system, pulling out entities that have both these components, but rather have every component in a vector of the component type. Also, how would the systems know 2 components belong to the same Entity? Will the components have an EntityID attached to it?

Edit: I just realized something, if RenderSystem loops through all RenderComponents and gets the entity, then checks if that entity also contains the TransformComponent, then it can render it. But that kinda ruins the whole "Systems don't care about the entity", this also requires that the component has an EntityID which I am still unsure if it should have. Someone please clarify all this for me.

Upvotes: 0

Views: 589

Answers (1)

Mathieu Van Nevel
Mathieu Van Nevel

Reputation: 1486

This part is call Entity Handler, and it's something which can need a lot of work.

The simplest form that i know is something like that:

Move every Components into one big array (cache is King). You'll need an enum for each Component:

enum E_Component {

  Velocity,
  Position,
  ...
};

And now your Entity is just an index and an array of component index.

struct Entity {

  unsigned index;
  unsigned components[NB_COMPONENTS]; // -1 if nothing
};

So if a System need to update the velocity component, all you need to do is to loop through your entities, check if there is a velocity component and update it.

==> System don't care about entities, only about components.


There is a lot of better way to do it, take a look at this article

Upvotes: 1

Related Questions