2025-05-24
entity-component-system pattern
The Entity-Component-System (ECS) pattern is an architectural pattern for representing game objects. Simple game engines use game loops with tightly coupled update methods that require specific call order. This creates hard-to-read, unmaintainable code. A really good description of adapating game loop style program to ECS: https://www.richardlord.net/blog/ecs/what-is-an-entity-framework. ECS separates data from behavior. Three core types: Entities, Components, Systems. Entities are unique identifiers, typically integers (ID: 42). They group related components. Components contain pure data - coordinates, health values, colors. No behavior or logic. Systems contain all logic and behavior. Systems query entities with specific component combinations. MovementSystem processes entities with Position and Velocity components. RenderSystem processes entities with Position and Sprite components. Composition-based approach provides key benefits:
Cache-friendly data layouts improve performance Mix components to create entity types without inheritance Systems operate on different data sets enabling parallelization
Example: bullet entity has Position, Velocity, Damage components. MovementSystem updates position. CollisionSystem detects hits. ECS seems complex initially but scales well. The data-oriented design and loose coupling makes large codebases easier to maintain and optimize. Modern game engines use ECS as their foundation. It's gaining adoption in performance-critical applications where flexible composition and high throughput matter.
sfsim uses ECS to represent simulation objects. (https://github.com/samif0/sfsim)