Entity component system

Short description: Software architectural pattern mostly used in video game development

Entity component system (ECS) is a software architectural pattern mostly used in video game development for the representation of game world objects. An ECS comprises entities composed from components of data, with systems which operate on entities' components.

ECS follows the principle of composition over inheritance, meaning that every entity is defined not by a type hierarchy, but by the components that are associated with it. Systems act globally over all entities which have the required components.

## Contents

* 1 Characteristics
* 2 History
* 3 Variations
* 3.1 Martin's ECS
* 3.2 The Unity game engine
* 3.3 Apparatus ECS
* 3.4 FLECS
* 4 Common patterns in ECS use
* 5 Debate
* 5.1 Is "system" first class?
* 5.2 Is ECS a useful concept?
* 6 See also
* 7 References
* 8 External links

## Characteristics

Entity: An entity represents a general-purpose object. In a game engine context, for example, every coarse game object is represented as an entity. Usually, it only consists of a unique id. Implementations typically use a plain integer for this.[1]

Component: A component labels an entity as possessing a particular aspect, and holds the data needed to model that aspect. For example, every game object that can take damage might have a Health component associated with its entity. Implementations typically use structs, classes, or associative arrays.[1]

System: A system is a process which acts on all entities with the desired components. For example, a physics system may query for entities having mass, velocity and position components, and iterate over the results doing physics calculations on the sets of components for each entity.

The behavior of an entity can be changed at runtime by systems that add, remove or modify components. This eliminates the ambiguity problems of deep and wide inheritance hierarchies often found in Object Oriented Programming techniques that are difficult to understand, maintain, and extend. Common ECS approaches are highly compatible with, and are often combined with, data-oriented design techniques. Data for all instances of a component are commonly stored together in physical memory, enabling efficient memory access for systems which operate over many entities.

## History

In 2007, the team working on Operation Flashpoint experimented with ECS designs, including those inspired by Bilas/Dungeon Siege, and Adam Martin later wrote a detailed account of ECS design,[2] including definitions of core terminology and concepts.[3] In particular, Martin's work popularized the ideas of systems as a first-class element, entities as identifiers, components as raw data, and code stored in systems, not in components or entities.

In 2015, Apple Inc. introduced GameplayKit, an API framework for iOS, macOS and tvOS game development that includes an implementation of ECS.[4]

In August 2018 Sander Mertens created the popular flecs ECS framework.[5]

In October 2018[6] the company Unity released its megacity demo that utilized a tech stack built on an ECS. It had 100,000 audio sources—one for every car, neon sign, and more—creating a large, complex soundscape.[6]

## Variations

The data layout of different ECSs can differ as well as the definition of components, how they relate to entities, and how systems access entities' components.

### Martin's ECS

A popular blog series by Adam Martin defines what he considers an Entity Component System architecture:[3]

An entity only consists of an ID for accessing components. It is a common practice to use a unique ID for each entity. This is not a requirement, but it has several advantages:

* The entity can be referred using the ID instead of a pointer. This is more robust, as it would allow for the entity to be destroyed without leaving dangling pointers.
* It helps for saving state externally. When the state is loaded again, there is no need for pointers to be reconstructed.
* Data can be shuffled around in memory as needed.
* Entity ids can be used when communicating over a network to uniquely identify the entity.

Some of these advantages can also be achieved using smart pointers.

Components have no game code (behavior) inside of them. The components don't have to be located physically together with the entity, but should be easy to find and access using the entity.

"Each System runs continuously (as though each System had its own private thread) and performs global actions on every Entity that possesses a Component or Components that match that System's query."

### The Unity game engine

Unity's layout has tables each with columns of components. In this system an entity type is based on the components it holds. For every entity type there is a table (called an archetype) holding columns of components that match the components used in the entity. To access a particular entity one must find the correct archetype (table) and index into each column to get each corresponding component for that entity.

### Apparatus ECS

Apparatus is a third-party ECS implementation for Unreal Engine that has introduced some additional features to the common ECS paradigm. One of those features is the support of the type hierarchy for the components. Each component can have a base component type (or a base class) much like in OOP. A system can then query with the base class and get all of its descendants matched in the resulting entities selection. This can be very useful for some common logic to be implemented on a set of different components and adds an additional dimension to the paradigm.

### FLECS

Flecs is a fast and lightweight ECS implementation for C & C++ that lets you build games and simulations with millions of entities.

## Common patterns in ECS use

The normal way to transmit data between systems is to store the data in components, and then have each system access the component sequentially. For example, the position of an object can be updated regularly. This position is then used by other systems. If there are a lot of different infrequent events, a lot of flags will be needed in one or more components. Systems will then have to monitor these flags every iteration, which can become inefficient. A solution could be to use the observer pattern. All systems that depend on an event subscribe to it. The action from the event will thus only be executed once, when it happens, and no polling is needed.

The ECS architecture has no trouble with dependency problems commonly found in Object Oriented Programming since components are simple data buckets, they have no dependencies. Each system will typically query the set of components an entity must have for the system to operate on it. For example, a render system might register the model, transform, and drawable components. When it runs, the system will perform its logic on any entity that has all of those components. Other entities are simply skipped, with no need for complex dependency trees. However this can be a place for bugs to hide, since propagating values from one system to another through components may be hard to debug. ECS may be used where uncoupled data needs to be bound to a given lifetime.

The ECS architecture uses composition, rather than inheritance trees. An entity will be typically made up of an ID and a list of components that are attached to it. Any game object can be created by adding the correct components to an entity. This allows the developer to easily add features of one object to another, without any dependency issues. For example, a player entity could have a bullet component added to it, and then it would meet the requirements to be manipulated by some bulletHandler system, which could result in that player doing damage to things by running into them.

The merits of using ECSs for storing the game state have been proclaimed by many game developers like Adam Martin. One good example is the blog posts by Richard Lord where he discusses the merits and why ECS designed game data storage systems are so useful.[7]

## Debate

### Is "system" first class?

This article defines ECS as a software architecture pattern with three first-class parts: entities, components, and systems.

Due to an ambiguity in the English language, however, a common interpretation of the name is that an ECS is a system comprising entities and components. For example, in the 2013 talk at GDC,[8] Scott Bilas compares a C++ object system and his new custom component system. This is consistent with a traditional use of system term in general systems engineering with Common Lisp Object System and type system as examples. Therefore, the idea of "Systems" as first-class elements is a contestable one.

The practical difference in such an entity-component architecture is that behaviors will be defined on the components and/or entities. This will have trade-offs making it more or less suitable depending on the application.

To avoid ambiguity in this article, we follow the words "Entity Component System" with a noun such as "framework" or "architecture". The word "system" is singular in this context.

### Is ECS a useful concept?

ECS combines orthogonal, well-established ideas in general computer science and programming language theory. For example, components can be seen as a mixin idiom in various programming languages. Components are a specialized case under the general delegation (object-oriented programming) approach and meta-object protocol. That is, any complete component object system can be expressed with the templates and empathy model within The Orlando Treaty[9] vision of object-oriented programming. But whatever the theoretical utility of the concept is, the widespread use of Entity Component System frameworks, particularly in games programming, make its practical utility indisputable.

## See also

* Model–view–controller
* Observer pattern
* Strategy pattern
* Relational model

## References

1. ↑ 1.0 1.1 "Entity Systems Wiki". http://entity-systems.wikidot.com/.
2. ↑ Martin, Adam. "Entity Systems are the Future of MMOG Development". http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/.
3. ↑ 3.0 3.1 Martin, Adam. "Entity Systems are the Future of MMOG Development Part 2". http://t-machine.org/index.php/2007/11/11/entity-systems-are-the-future-of-mmog-development-part-2/.
4. ↑ "Introducing GameplayKit - WWDC 2015 - Videos". https://developer.apple.com/videos/play/wwdc2015/608/.
5. ↑ "SanderMertens - Overview" (in en). https://github.com/SanderMertens.
6. ↑ 6.0 6.1 "Unity unleashes Megacity demo - millions of objects in a huge cyberpunk world" (in en-GB). MCV/DEVELOP. 2018-10-24. https://www.mcvuk.com/development-news/unity-unleashes-megacity-demo-millions-of-objects-in-a-huge-cyberpunk-world/. Retrieved 2021-06-24.
7. ↑ "Why use an Entity Component System architecture for game development?". https://www.richardlord.net/blog/ecs/why-use-an-entity-framework.html.
8. ↑ Bilas, Scott. "A Data-Driven Game Object System". http://gamedevs.org/uploads/data-driven-game-object-system.pdf.
9. ↑ Lynn Andrea Stein, Henry Liberman, David Ungar: A shared view of sharing: The Treaty of Orlando. In: Won Kim, Frederick H. Lochovsky (Eds.): Object-Oriented Concepts, Databases, and Applications ACM Press, New York 1989, ch. 3, pp. 31–48 ISBN:0-201-14410-7 (online )

## External links

* Anatomy of a knockout
* Evolve Your Hierarchy
* Entity Systems Wiki
* Component - Game Programming Patterns
* ECS design to achieve true Inversion of Flow Control

* v
* t
* e

Software design patterns

Creational|

* Abstract factory
* Builder
* Dependency injection
* Factory method
* Lazy initialization
* Multiton
* Object pool
* Prototype
* RAII
* Singleton

Structural|

* Adapter
* Bridge
* Composite
* Decorator
* Delegation
* Facade
* Flyweight
* Front controller
* Marker interface
* Module
* Proxy
* Twin

Behavioral|

* Blackboard
* Chain of responsibility
* Command
* Interpreter
* Iterator
* Mediator
* Memento
* Null object
* Observer
* Servant
* Specification
* State
* Strategy
* Template method
* Visitor

Functional|

* Closure
* Currying
* Function composition
* Functor
* Monad
* Generator

Concurrency|

* Active object
* Actor
* Balking
* Barrier
* Binding properties
* Coroutine
* Compute kernel
* Double-checked locking
* Event-based asynchronous
* Fiber
* Futex
* Futures and promises
* Guarded suspension
* Immutable object
* Join
* Lock
* Messaging
* Monitor
* Nuclear
* Proactor
* Reactor
* Read write lock
* Scheduler
* Thread pool
* Thread-local storage

Architectural|

* ADR
* Active record
* Broker
* Client–server
* CBD
* DAO
* DTO
* DDD
* ECB
* ECS
* EDA
* Front controller
* Identity map
* Interceptor
* Implicit invocation
* Inversion of control
* Model 2
* MOM
* Microservices
* MVA
* MVC
* MVP
* MVVM
* Monolithic
* Multitier
* Naked objects
* ORB
* P2P
* Publish–subscribe
* PAC
* REST
* SOA
* Service locator
* SN
* SBA
* Specification

Cloud
Distributed|

* Ambassador
* Anti-Corruption Layer
* Bulkhead
* Cache-Aside
* Circuit Breaker
* CQRS
* Compensating Transaction
* Competing Consumers
* Compute Resource Consolidation
* Event Sourcing
* External Configuration Store
* Federated Identity
* Gatekeeper
* Index Table
* Leader Election
* MapReduce
* Materialized View
* Pipes
* Filters
* Priority Queue
* Publisher-Subscriber
* Queue-Based Load Leveling
* Retry
* Scheduler Agent Supervisor
* Sharding
* Sidecar
* Strangler
* Throttling
* Valet Key

Other|

* Business delegate
* Composite entity
* Intercepting filter
* Lazy loading
* Mangler
* Mock object
* Type tunnel
* Method chaining

Books|

* Design Patterns
* Enterprise Integration Patterns

People|

* Christopher Alexander
* Erich Gamma
* Ralph Johnson
* John Vlissides
* Grady Booch
* Kent Beck
* Ward Cunningham
* Martin Fowler
* Robert Martin
* Jim Coplien
* Douglas Schmidt
* Linda Rising

Communities|

* The Hillside Group
* The Portland Pattern Repository

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Entity component system. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Entity_component_system&oldid=3009279"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template