Reading — step 1 of 5
Read
~1 min readEntity-Component System
Entity-Component System (ECS)
Object-oriented games typically use class hierarchies:
Entity
├── Player(health, speed)
├── Enemy(health, attack)
└── Bullet(velocity, damage)
Problems:
- Class explosion (PoisonedFlyingPlayer?).
- Diamond inheritance.
- Cache-unfriendly (each entity is allocation).
ECS: composition over inheritance.
- Entity: just an ID (integer).
- Component: pure data, attached to entities.
- System: function operating on entities with specific components.
python
Systems:
python
Pros:
- Composition: any combination of components.
- Cache-friendly: store components in arrays (SoA).
- Parallelizable: systems independent.
- Simple to add/remove behavior at runtime.
Cons:
- Indirection (entity ID → components).
- Less natural for traditional OOP folks.
Modern ECS frameworks:
- Bevy (Rust): default.
- EnTT (C++): widely-used.
- flecs (C): standalone.
- Unity DOTS: Unity's ECS (in transition).
Archetype-based ECS:
- Group entities by exact set of components.
- Each archetype = an array of entities + arrays of components.
- Iteration is contiguous memory access.
- Faster than dict-based.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…