Entity–component–system
   HOME

TheInfoList



OR:

Entity–component–system (ECS) is a software architectural pattern mostly used in
video game development Video game development (sometimes shortened to gamedev) is the process of creating a video game. It is a multidisciplinary practice, involving programming, design, art, audio, user interface, and writing. Each of those may be made up of more speci ...
for the representation of game world objects. An ECS comprises ''entities'' composed from ''components'' of data, with ''systems'' which operate on the components. ECS follows the principle of
composition over inheritance Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should favor Polymorphism (computer science), polymorphic behavior and code reuse by their object composition, compositi ...
, 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. Especially when written “Entity Component System”, due to an ambiguity in the English language, a common interpretation of the name is that an ECS is a system comprising entities and components. For example, in the 2002 talk at GDC, 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 Systems engineering is an interdisciplinary field of engineering and engineering management that focuses on how to design, integrate, and manage complex systems over their Enterprise life cycle, life cycles. At its core, systems engineering uti ...
with
Common Lisp Object System The Common Lisp Object System (CLOS) is the facility for object-oriented programming in American National Standards Institute, ANSI Common Lisp. CLOS is a powerful dynamic programming language, dynamic object system which differs radically from t ...
and
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
as examples.


Characteristics

ECS combines orthogonal, well-established ideas in general
computer science Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
and
programming language theory Programming language theory (PLT) is a branch of computer science that deals with the design, implementation, analysis, characterization, and classification of formal languages known as programming languages. Programming language theory is clos ...
. For example, components can be seen as a
mixin In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depe ...
idiom in various programming languages. Components are a specialized case under the general
delegation Delegation is the process of distributing and entrusting work to another person.Schermerhorn, J., Davidson, P., Poole, D., Woods, P., Simon, A., & McBarron, E. (2017). ''Management'' (6th ed., pp. 282–286). Brisbane: John Wiley & Sons Australia. ...
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 vision of
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
. 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. Component: A component characterizes 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 array In computer science, an associative array, key-value store, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In math ...
s. 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 set 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 Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
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 contiguously stored together in physical memory, enabling efficient memory access for systems which operate over many entities.


History

In 1998, Thief: The Dark Project pioneered an ECS. The engine was later used for its sequel, as well as
System Shock 2 ''System Shock 2'' is a 1999 action role-playing survival horror video game designed by Ken Levine and co-developed by Irrational Games and Looking Glass Studios. Originally intended to be a standalone title, its story was changed during produ ...
. In 2002, Scott Bilas of Gas Powered Games (Dungeon Siege) gave a seminal talk on ECS. This inspired numerous later well-known implementations. In early January 2007, Mick West who worked on the Tony Hawk series, shared his experiences on the process of ECS adoption at Neversoft. Also in 2007, the team working on '' Operation Flashpoint: Dragon Rising'' experimented with ECS designs, including those inspired by Bilas/''Dungeon Siege'', and Adam Martin later wrote a detailed account of ECS design, including definitions of core terminology and concepts. 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. Apple Inc. is an American multinational corporation and technology company headquartered in Cupertino, California, in Silicon Valley. It is best known for its consumer electronics, software, and services. Founded in 1976 as Apple Comput ...
introduced GameplayKit, an
API An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
framework for
iOS Ios, Io or Nio (, ; ; locally Nios, Νιός) is a Greek island in the Cyclades group in the Aegean Sea. Ios is a hilly island with cliffs down to the sea on most sides. It is situated halfway between Naxos and Santorini. It is about long an ...
,
macOS macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
and
tvOS tvOS (formerly Apple TV Software) is an operating system developed by Apple for the Apple TV, a digital media player. In the first-generation Apple TV, Apple TV Software was based on Mac OS X. The software for the second-generation and later ...
game development that includes an implementation of ECS. In October 2018 the company Unity released its megacity demo that utilized a tech stack built on an ECS. Unity's ECS runs on a powerful optimised architecture known as DOTS which "empowers creators to scale processing in a highly performant manner".


Variations

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


Martin's ECS

Adam Martin defines in his blog series what he considers an Entity–Component–System. 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 pointer In computer science, a smart pointer is an abstract data type that simulates a pointer while providing added features, such as automatic memory management or bounds checking. Such features are intended to reduce bugs caused by the misuse of p ...
s. 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.


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 In software design and software engineering, the observer pattern is a software design pattern in which an object, called the ''subject'' (also known as ''event source'' or ''event stream''), maintains a list of its dependents, called observers (a ...
. 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 has no trouble with dependency problems commonly found in
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
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 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 to an entity, 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.


Usage outside of games

Although mostly found in video game development, the ECS can be useful in other domains.Romeo, Vittorio. (2016): ''Analysis of entity encoding techniques, design and implementation of a multithreaded compile-time Entity-Component-System C++14 library'' 10.13140/RG.2.1.1307.4165. (https://www.researchgate.net/publication/305730566_Analysis_of_entity_encoding_techniques_design_and_implementation_of_a_multithreaded_compile-time_Entity-Component-System_C14_library)


See also

*
Model–view–controller Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divides the related program logic into three interconnected elements. These elements are: * the model, the internal representat ...
*
Observer pattern In software design and software engineering, the observer pattern is a software design pattern in which an object, called the ''subject'' (also known as ''event source'' or ''event stream''), maintains a list of its dependents, called observers (a ...
*
Strategy pattern In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives runtime ins ...
*
Relational model The relational model (RM) is an approach to managing data using a structure and language consistent with first-order predicate logic, first described in 1969 by English computer scientist Edgar F. Codd, where all data are represented in terms of t ...


Notes


References


External links


Anatomy of a knockout

Evolve Your Hierarchy

Entity Systems Wiki



ECS design to achieve true Inversion of Flow Control
{{DEFAULTSORT:Entity-component-system Architectural pattern (computer science) Software design patterns