Flecs: Flecs
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.0 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
Flecs

Flecs is a fast and lightweight Entity Component System that lets you build games and simulations with millions of entities (join the Discord!). Here are some of the framework's highlights:

- Fast and portable zero dependency C99 API
- Modern type-safe C++11 API that doesn't use STL containers
- First open source ECS with full support for Entity Relationships!
- Fast native support for hierarchies and prefabs
- Code base that builds in less than 5 seconds
- Runs in the browser without modifications with emscripten
- Cache friendly archetype/SoA storage that can process millions of entities every frame
- Automatic component registration that works out of the box across shared libraries/DLLs
- Write free functions with queries or run code automatically in systems
- Run games on multiple CPU cores with a fast lockless scheduler
- Verified on all major compilers and platforms with CI running more than 10000 tests
- Integrated reflection framework with JSON serializer and support for runtime components
- Unit annotations for components
- Powerful query language with support for joins and inheritance
- Statistics addon for profiling ECS performance
- A web-based UI for monitoring & controlling your apps:

Flecs Explorer

To support the project, give it a star 🌟 !

## What is an Entity Component System?

ECS is a way of organizing code and data that lets you build games that are larger, more complex and are easier to extend. Something is called an ECS when it:

- Has entities that uniquely identify objects in a game
- Has components which are datatypes that can be added to entities
- Has systems which are functions that run for all entities matching a component query

For more information, check the ECS FAQ!

## Show me the code!

C99 example:

typedef struct {
float x, y;
} Position, Velocity;
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 0);
Velocity *v = ecs_field(it, Velocity, 1);
for (int i = 0; i < it->count; i++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init();
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Velocity);
ECS_SYSTEM(ecs, Move, EcsOnUpdate, Position, Velocity);
ecs_entity_t e = ecs_insert(ecs,
ecs_value(Position, {10, 20}),
ecs_value(Velocity, {1, 2}));
while (ecs_progress(ecs, 0)) { }
}
EcsOnUpdate
const ecs_entity_t EcsOnUpdate
OnUpdate pipeline phase.
ecs_progress
FLECS_API bool ecs_progress(ecs_world_t *world, ecs_ftime_t delta_time)
Progress a world.
ECS_SYSTEM
#define ECS_SYSTEM(world, id, phase,...)
Declare & define a system.
Definition system.h:220
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:381
ecs_world_t
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:425
ECS_COMPONENT
#define ECS_COMPONENT(world, id)
Declare & define a component.
Definition flecs_c.h:145
ecs_value
#define ecs_value(T,...)
Convenience macro for creating compound literal value literal.
Definition flecs_c.h:738
ecs_init
ecs_world_t * ecs_init(void)
Create a new world.
ecs_iter_t
Iterator.
Definition flecs.h:1136
ecs_iter_t::count
int32_t count
Number of entities to iterate.
Definition flecs.h:1186

Same example in C++11:

struct Position {
float x, y;
};
struct Velocity {
float x, y;
};
int main(int argc, char *argv[]) {
flecs::world ecs;
ecs.system<Position, const Velocity>()
.each([](Position& p, const Velocity& v) {
p.x += v.x;
p.y += v.y;
});
auto e = ecs.entity()
.insert([](Position& p, Velocity& v) {
p = {10, 20};
v = {1, 2};
});
while (ecs.progress()) { }
}
flecs::world::system
flecs::system system(flecs::entity e) const
Upcast entity to a system.
Definition impl.hpp:114
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:206
flecs::world::progress
bool progress(ecs_ftime_t delta_time=0.0) const
Progress world one tick.
Definition impl.hpp:47
flecs::entity_builder::insert
const Self & insert(const Func &func) const
Set 1..N components.
Definition impl.hpp:23
flecs::world
The world.
Definition world.hpp:137
flecs::world::each
void each(Func &&func) const
Iterate over all entities with components in argument list of function.

## Projects using Flecs

If you have a project you'd like to share, let me know on Discord!

### Hytale

Hytale

> We knew that we wanted to build Hytale around an Entity-Component-System (ECS). When we analyzed the options, FLECS rose to the top. FLECS provides the backbone of the Hytale Game Engine. Its flexibility has allowed us to build highly varied gameplay while supporting our vision for empowering Creators.

– Dann Webster, Hypixel studios

### Tempest Rising

Tempest Rising

### Territory Control 2

image

### Resistance is Brutal

image

### Age of Respair

image

### The Forge

image

### Extermination Shock

image

### Tome Tumble Tournament

image

### Hyperion Minecraft Server

image

### Sol Survivor

image

### Equilibrium Engine

image

### After Sun

image

### Flecs Demo's

https://github.com/SanderMertens/tower_defense

Tower Defense

https://github.com/flecs-hub/city

City

## Flecs Hub

Flecs Hub is a collection of repositories that show how Flecs can be used to build game systems like input handling, hierarchical transforms and rendering.

Module | Description
--- | ---
flecs.components.cglm | Component registration for cglm (math) types
flecs.components.input | Components that describe keyboard and mouse input
flecs.components.transform | Components that describe position, rotation and scale
flecs.components.physics | Components that describe physics and movement
flecs.components.geometry | Components that describe geometry
flecs.components.graphics | Components used for computer graphics
flecs.components.gui | Components used to describe GUI components
flecs.systems.transform | Hierarchical transforms for scene graphs
flecs.systems.physics | Systems for moving objects and collision detection
flecs.systems.sokol | Sokol-based renderer
flecs.game | Generic game systems, like a camera controller

## Language bindings

The following language bindings have been developed with Flecs! Note that these are projects built and maintained by helpful community members, and may not always be up to date with the latest commit from master!

- C#:
  - BeanCheeseBurrito/Flecs.NET
- Rust:
  - Flecs-Rust
  - flecs-polyglot
  - flecs-rs
- Zig:
  - zig-gamedev/zflecs
- Lua:
  - sro5h/flecs-luajit
  - flecs-hub/flecs-lua