Data Driven Design, Entity-Component Systems, Properties Pattern, Prototype Data
# Notability
Often discussed heavily in game development circles, who typically have to deal with very complex relationships between data and behaviour.
May have direct implications for the future of general development outside of games.
# Definition
There are a lot of differing opinions about what ECS means and how it is used in practice.
> Confusingly, ECS and Entity-Component frameworks (EC) are not the same. EC frameworks, as typically found in game engines, are similar to ECS in that they allow for the creation of entities and the composition of components. However, in an EC framework, components are classes that contain both data and behavior, and behavior is executed directly on the component.
\- ECS FAQ
See also: [[Data Oriented Design]], [[Struct Array Inversion]]
# Notability
## Implemenations
- [[Unity Engine]] has multiple optional ECS-like systems
- ECS for Unity - https://unity.com/ecs
- DOTS - https://github.com/Unity-Technologies/EntityComponentSystemSamples
- Entitas - https://github.com/sschmid/Entitas
- Photon Quantum - https://doc.photonengine.com/quantum/current/quantum-intro
- Svelto - https://github.com/sebas77/Svelto.ECS
- [[Bevy]] uses ECS style for everything, including memory layout
- Flecs
- https://www.flecs.dev/flecs/
- https://github.com/SanderMertens/flecs
- EnTT - https://github.com/skypjack/entt
- HECS - https://github.com/Ralith/hecs
- Legion - https://github.com/amethyst/legion
- Shipyard - https://github.com/leudz/shipyard
- EntityX - https://github.com/alecthomas/entityx
- Specs Parallel ECS - https://github.com/amethyst/specs
# Parts of a Full ECS
## Entities
An entity is a pointer or other identifier. Components are associated with this identifier, perhaps loosely, and the identifier contains no information itself.
## Components
A component is a segment of discrete data. Ideally data of the same structure are collected together in a way that optimizes CPU caching and can be operated on as a group or in series.
## Systems
A system is a set of behaviors that operate on data structures and can be applied across many instances of a given structure.
# Parts of an OO EC
## Entities
The entity is an object which may have its own data and behaviour, but some or all of its functionality is described by the components it owns.
## Components
Components are subobjects with behaviour and data. The subobjects are typically arranged in an array inside the owning object.
# Extensions
## Tags
A component that has no data, but is nevertheless used as a way to treat entities differently.
## Query
Seems similar to the [[Command Pattern]]?
## Relationships
Relational mappings between entities.
# Approaches
The general pattern can be implemented in any language but the way it is implemented can vary wildly. The biggest issue is how to keep track of the list of components and how to navigate the matrix of entities and components.
The really compelling idea about an ECS is that in many situations, the "entity" is not important. For example, running through the AI rules of a bunch of NPCs in a game no longer requires iterating through a list of game objects and pulling their behaviour data, instead the AI manager just directly has the list of behaviour data.
Depending how it is implemented, it could be very confusing to trace execution and side effects at a distance.
## Dense
> stores entities in tables, where components are columns and entities are rows. Archetype implementations are fast to query and iterate.
## Sparse
> A sparse set based ECS stores each component in its own sparse set which is has the entity id as key. Sparse set implementations allow for fast add/remove operations.
## Bitset
> A bitset-based ECS stores components in arrays where the entity id is used as index, and uses a bitset to indicate if an entity has a specific component. Different flavors of bitset-based approaches exist. One approach is to have an array for each component with an accompanying bitset to indicate which entities have the component. Another approach uses the hibitset data structure
### Hibitset
> Provides hierarchical bit sets, which allow very fast iteration on sparse data structures.
https://docs.rs/hibitset/0.6.3/hibitset/
## Reactive
> A reactive ECS uses signals resulting from entity mutations to keep track of which entities match systems/queries.
A reactive ECS could be see as an event driven system.
# See Also
- [[Entity Component System]]
- [[Godot - Code Organization]]
## Locally Archived Articles
- [[Game Object Structure Inheritance vs. Aggregation (Game Architect)]]
- [[State of the Art Game Objects (GBGames)]]
- [[Object-Oriented Game Programming The Behavior System (Riverman Media)]]
- [[The Universal Design Pattern (Stevey's Blog Rants)]] (not the same, but related)
- [[Evolve Your Hierarchy (Cowboy Programming)]]
- [[Building Games in ECS with Entity Relationships (Sander Mertens)]]
# External Links
- https://en.wikipedia.org/wiki/Entity_component_system
- https://www.dataorienteddesign.com/dodbook/
- https://gamesfromwithin.com/data-oriented-design
- https://gameprogrammingpatterns.com/type-object.html
- http://gamearchitect.net/Articles/GameObjects1.html
- https://stackoverflow.com/questions/490570/what-are-the-advantages-and-disadvantages-of-the-properties-pattern
- [Exploring the Distinctions Between Value Objects and Entities in Object-Oriented Programming](https://mderis.medium.com/exploring-the-distinctions-between-value-objects-and-entities-in-object-oriented-programming-abab39cc5f42)
- https://github.com/SanderMertens/ecs-faq
- [Godot - Entity Component Pattern](https://www.gdquest.com/tutorial/godot/design-patterns/entity-component-pattern/)
## Components as Capabilities
```cardlink
url: https://youtu.be/JxI3Eu5DPwE
title: "Bob Nystrom - Is There More to Game Architecture than ECS?"
description: "Talk from the Roguelike Celebration 2018 - http://roguelike.club"
host: youtu.be
favicon: https://www.youtube.com/s/desktop/5e42dd8a/img/favicon_32x32.png
image: https://i.ytimg.com/vi/JxI3Eu5DPwE/maxresdefault.jpg
```