Skip to content

avikor/EntityComponentSystem

Repository files navigation

EntityComponentSystem

This repository contains an Entity Component System (ECS) implemented using C++20.

  • A Component is a Plain Old Data type (POD). In this implementation it's a Trivially Copyable type.
  • An Entity is a container of components. These components can be added or removed from the entity. In this implementation an entity is essentially a std::array of std::variant which stores std::unique_ptr to components.
    (There's also a struct version in which an Entity is a struct which directly holds pointers to components, without a std::array. This version is faster but also less safe and harder to maintain. Check out "struct_version" branch to see it.)
  • A System is a function which uses entities' components to perform a computation.

An entity component system "facilitates code reusability by separating the data from the behavior" (source). Components and entities handle the data. Systems handle the behavior.

Since all entities are of the same class, an entity may enroll in one or more groups in order to differentiate its behavior from other entities.
These groups can be used as an alternative to classes, since the user can write a system which handles only entities who are members of a specific group, thus differentiating their behavior from other entities.
Additionally, this implementation doesn't use inheritance so there is no overhead due dynamic dispatch.

This entity component system was built with the following ideas in mind:

  1. Principle of locality.
  2. Object pool design pattern.
  3. Entity Component System architectural pattern.
  4. Multithreading.

In short: It couples entity-component-system-architectural-pattern with object-pool-design-pattern to fully leverage principle-of-locality-based-optimizations performed by multiple threads. It does so by pooling both components and entities in object pools, and by executing the systems asynchronously.

Components and entities are allocated at compile time using their respective pools.
Each component type has its own pool, and all entities are allocated in a single entities pool.
Since an entity is essentially a std::array of std::unique_ptr to std::variant, iterating over an entity's components isn't as fast as iterating directly over all components of a specific type, since they are stored by their pool contiguously in memory.
The user of this repository is highly advised to design its components in a way such that when a system uses a component to perform its computation, it has all the data it needs in that component, rather than having to query for another component of that entity.
A good rule of thumb is that if a system needs two components to perform its computation, it's probably better to combine the two components into a single component.

Some toy examples are present at 'EntityComponentSystem/ecsTests.cpp'.
NOTE: this implementation is not entirely thread-safe, as the Entity class is not protected by a mutex.
The allocation and deallocation of components and entities is thread-safe however.

About

Entity Component System implemented using C++20

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published