Entity Component System - Pyrohax/Simpleton GitHub Wiki
Introduction
The Entity Component System (ECS) is currently setup as follows.
Components
Each component in Simpleton is simply a class that contains data. The members of the components are currently not particularly cache-friendly and memory aligned. We'll decide if we want to do that later. Every component inherits from a Component class, for easier type checking. The component class is empty and contains no virtual functions, which means the size of a inherited component will be equal to the combined size of their member variables.
Adding a new component type requires three changes in the ECS, besides needing to inherit from the Component base class:
- Add the new component type to the enum class ComponentType.
- Link the component to the enum class in GetTypeByComponentClass().
- Implement a delete function with an appropriate static cast in EntityComponentSystem::RemoveComponent.
Entities
Enities are currently simply a class that holds pointers to components, for easy access and simple-to-write game code. We might decide on changing this later as well, if we notice that other approaches are more fitting.
Usage
Usage of the ECS is still under discussion, as we'll need to write some game code to find out what works best for making the different systems to update our components multithreadable, efficient, and easy to use. The current implementation is meant to be used roughly as follows:
Game code
UniqueID entityID = AddEntity();
Entity* entityPtr = GetEntity(entityID);
UniqueID componentID = AddComponent<TransformComponent>();
TransformComponent* componentPtr = GetComponent<TransformComponent>(componentID);
entityPtr->myComponentIDList.push_back(componentID);
componentPtr->SetPos(componentPtr->GetPos() + glm::vec3(100, 0, 0));
Keep in mind that using the ID will always give you the right value, although the data behind the pointer to the entity or component might change from frame to frame (in case of array resizes, for example).
Future goals
We are thinking of creating a system per component that contains a list for each member in the component type, to improve cache friendliness. We would then have a pointer to an element in the list instead of the member in the component type, which points to the right element in that member's list.