Entityx
EntityX - A fast, type-safe C++ Entity-Component system
Install / Use
/learn @alecthomas/EntityxREADME
EntityX - A fast, type-safe C++ Entity Component System
NOTE: The current stable release 1.0.0 breaks backward compatibility with < 1.0.0. See the change log for details.
Entity Component Systems (ECS) are a form of decomposition that completely decouples entity logic and data from the entity "objects" themselves. The Evolve your Hierarchy article provides a solid overview of EC systems and why you should use them.
EntityX is an EC system that uses C++11 features to provide type-safe component management, event delivery, etc. It was built during the creation of a 2D space shooter.
Downloading
You can acquire stable releases here.
Alternatively, you can check out the current development version with:
git clone https://github.com/alecthomas/entityx.git
See below for installation instructions.
Contact
Feel free to jump on my Gitter channel if you have any questions/comments. This is a single channel for all of my projects, so please mention you're asking about EntityX to avoid (my) confusion.
You can also contact me directly via email or Twitter.
Benchmarks / Comparisons
EntityX includes its own benchmarks, but @abeimler has created a benchmark suite testing up to 2M entities in EntityX, the EntityX compile-time branch, Anax, and Artemis C++.
Recent Notable Changes
- 2014-03-02 - (1.0.0alpha1) Switch to using cache friendly component storage (big breaking change). Also eradicated use of
std::shared_ptrfor components. - 2014-02-13 - Visual C++ support thanks to Jarrett Chisholm!
- 2013-10-29 - Boost has been removed as a primary dependency for builds not using python.
- 2013-08-21 - Remove dependency on
boost::signaland switch to embedded Simple::Signal. - 2013-08-18 - Destroying an entity invalidates all other references
- 2013-08-17 - Python scripting, and a more robust build system
See the ChangeLog for details.
EntityX extensions and example applications
- Will Usher has also written an Asteroids clone.
- Roc Solid Productions have written a space shooter!
- Giovani Milanez's first game.
- A game using Ogre3D and EntityX.
DEPRECATED - 0.1.x ONLY
- Wu Zhenwei has written Lua bindings for EntityX, allowing entity logic to be extended through Lua scripts.
- Python bindings allowing entity logic to be extended through Python scripts.
- Rodrigo Setti has written an OpenGL Asteroids clone which uses EntityX.
Example
An SFML2 example application is available that shows most of EntityX's concepts. It spawns random circles on a 2D plane moving in random directions. If two circles collide they will explode and emit particles. All circles and particles are entities.
It illustrates:
- Separation of data via components.
- Separation of logic via systems.
- Use of events (colliding bodies trigger a CollisionEvent).
Compile with:
c++ -O3 -std=c++11 -Wall -lsfml-system -lsfml-window -lsfml-graphics -lentityx example.cc -o example
Or enable the CMake option ENTITYX_ENABLE_EXAMPLE_TARGET to enable the 'entityx_example' target which fetches SFML and builds the example.
Overview
In EntityX data associated with an entity is called a entityx::Component. Systems encapsulate logic and can use as many component types as necessary. An entityx::EventManager allows systems to interact without being tightly coupled. Finally, a Manager object ties all of the systems together for convenience.
As an example, a physics system might need position and mass data, while a collision system might only need position - the data would be logically separated into two components, but usable by any system. The physics system might emit collision events whenever two entities collide.
Tutorial
Following is some skeleton code that implements Position and Direction components, a MovementSystem using these data components, and a CollisionSystem that emits Collision events when two entities collide.
To start with, add the following line to your source file:
#include "entityx/entityx.h"
Entities
An entityx::Entity is a convenience class wrapping an opaque uint64_t value allocated by the entityx::EntityManager. Each entity has a set of components associated with it that can be added, queried or retrieved directly.
Creating an entity is as simple as:
#include <entityx/entityx.h>
entityx::EntityX ex;
entityx::Entity entity = ex.entities.create();
And destroying an entity is done with:
entity.destroy();
Implementation details
- Each
entityx::Entityis a convenience class wrapping anentityx::Entity::Id. - An
entityx::Entityhandle can be invalidated withinvalidate(). This does not affect the underlying entity. - When an entity is destroyed the manager adds its ID to a free list and invalidates the
entityx::Entityhandle. - When an entity is created IDs are recycled from the free list first, before allocating new ones.
- An
entityx::EntityID contains an index and a version. When an entity is destroyed, the version associated with the index is incremented, invalidating all previous entities referencing the previous ID. - To improve cache coherence, components are constructed in contiguous memory ranges by using
entityx::EntityManager::assign<C>(id, ...).
Components (entity data)
The general idea with the EntityX interpretation of ECS is to have as little logic in components as possible. All logic should be contained in Systems.
To that end Components are typically POD types consisting of self-contained sets of related data. Components can be any user defined struct/class.
Creating components
As an example, position and direction information might be represented as:
struct Position {
Position(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
float x, y;
};
struct Direction {
Direction(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
float x, y;
};
Assigning components to entities
To associate a component with a previously created entity call entityx::Entity::assign<C>() with the component type, and any component constructor arguments:
// Assign a Position with x=1.0f and y=2.0f to "entity"
entity.assign<Position>(1.0f, 2.0f);
Querying entities and their components
To query all entities with a set of components assigned you can use two methods. Both methods will return only those entities that have all of the specified components associated with them.
entityx::EntityManager::each(f) provides functional-style iteration over
entity components:
entities.each<Position, Direction>([](Entity entity, Position &position, Direction &direction) {
// Do things with entity, position and direction.
});
For iterator-style traversal of components, use
entityx::EntityManager::entities_with_components():
ComponentHandle<Position> position;
ComponentHandle<Direction> direction;
for (Entity entity : entities.entities_with_components(position, direction)) {
// Do things with entity, position and direction.
}
To retrieve a component associated with an entity use entityx::Entity::component<C>():
ComponentHandle<Position> position = entity.component<Position>();
if (position) {
// Do stuff with position
}
Component dependencies
In the case where a component has dependencies on other components, a helper class exists that will automatically create these dependencies.
eg. The following will also add Position and Direction components when a Physics component is added to an entity.
#include "entityx/deps/Dependencies.h"
system_manager->add<entityx::deps::Dependency<Physics, Position, Direction>>();
Implementation notes
- Components must provide a no-argument constructor.
- The default implementation can handle up to 64 components in total. This can be extended by changing the
entityx::EntityManager::MAX_COMPONENTSconstant. - Each type of component is allocated in (mostly) contiguous blocks to improve cache coherency.
Systems (implementing behavior)
Systems implement behavior using one or more components. Implementations are subclasses of System<T> and must implement the update() method, as shown below.
A basic movement system might be implemented with something like the following:
struct MovementSystem : public System<MovementSystem> {
void update(entityx::EntityManager &es, entityx::EventManager &events, TimeDelta dt) override {
es.each<Position, Direction>([dt](Entity entity, Position &position, Direction &direction) {
position.x += direction.x
Related Skills
node-connect
340.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
340.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.2kCommit, push, and open a PR


