Component Based Entities - HopsonCommunity/Community-Game GitHub Wiki

What is an entity?

"Let's think of an entity like a key."

Think of an entity like an object with a unique id and components as the teeth of a key

"Keys also have teeth (dark blue). The teeth of our entity key is the components that make it up. You can tell entities apart by their ID, even if they have the same teeth. So what do keys fit into? Locks. Locks are our systems. For example, a movement system."

Components are just a way to store some variables. You can look examples of them in the source code. Very simple.

What is a system?

An example of a movement system which is very similar to the one we use in this project.

A System is responsible for the behaviour of entities. Let's look at an example in code:

class System
{
public:
    virtual void update(const Timestep& ts, Entity* entity) {};
};

_The base class for systems. Each system has an update method which takes two parameters:

  1. Timestep: the time that has passed since the last update a.k.a delta time
  2. A pointer to the entity which is currently processed. (We will look at this later)_

Now let's look at an implementation of a system. The movement system's update method:

Sorry for the dark theme.

Now that's good. We now know what entities are, how systems are implemented and how they work. But where are entities and system updated?!? The Level class.

The level class has a vector of all the entities that are in the level. It also has a vector of all the systems that are active.

Systems are added in the Level constructor:

m_updateSystems.push_back(std::make_unique<Framework::MoveSystem>());

Each update of level the following code is called:

for (uint i = 0; i < m_entities.size(); i++)
    if (m_entities[i] != nullptr)
    {
        Framework::Entity* e = m_entities[i];
	for (auto& system : m_updateSystems)
	    system->update(ts, e);
    e->update(ts);
}

What is happening here? Each update we loop through all the entities that are in the level and for each of them we feed each system the entity. If you look above you can see an example of a system update method. The system then modifies entities components in whatever way it wants.

Now RenderSystem is a special case. It is stored in level outside the vector of all other systems and it's called in the render method in the level class. All other systems are called 60 times a second while rendering needs to occur as fast as possible.

Thanks for reading! I hope it is now clear how the entity system is designed and how entity systems operate.

The whole idea for the system + the analogy used: http://gamedev.stackexchange.com/questions/31473/what-is-the-role-of-systems-in-a-component-based-entity-architecture