Video games are a serious business. Ask anyone who plays or designs them! A lot goes into creating these games, including various utilities, architecture, and coding. That's why today we are covering Entity Component Systems. Some writers insert hyphens between each word, but we won't be doing that, although we will often refer to the term by its commonly used acronym of ECS.

This article introduces you to the Entity Component System, including what it is, its pros and cons, examples, how to create ECS hierarchies, and much more.

Let’s begin our guide with some definitions.

What Is an Entity Component System?

The Entity Component System is an architectural pattern often used in video game development. It facilitates code reusability by separating the data from the behavior. In addition, ECS obeys the "composition over inheritance principle," providing improved flexibility and helping developers identify entities in a game's scene where all the objects are classified as entities.

Frameworks often enable Entity Component Systems, and the term "ECS" is frequently used to illustrate a specific design pattern implementation.

An ECS consists of the following elements:

  • It has unique identifiers known as entities.
  • It contains plain datatypes without behavior known as components.
  • It has systems, defined as functions that are matched with entities that have a particular set of components.
  • Entities may contain zero or more components.
  • Entities can dynamically change components.

So, an ECS is an architecture that focuses on data and separates data/components, identity/entities, and behavior/systems. These characteristics make it a natural fit for video game design.

Now, let’s break down the term into separate parts and identify them better.

What Is Defined as an Entity?

An entity represents one "thing" in a video game, a distinct object representing an actor in a simulated space, typically expressed as a unique integer value. For example, if you’re playing Skyrim, all the tangible, visible “things” in the game’s universe are entities. They contain no actual data or behaviors.

What’s the Definition of a Component?

Components are datatypes consisting of a unique behavior assigned to an entity. They are reusable modules that programmers attach to the entities, providing behavior, functionality, and appearance, forming an entity.

For example, a sword and sorcery game programmer could build a magic sword entity by gathering these components:

  • A material component, such as "shininess," influences the sword's appearance
  • A weight component measures “pounds” to determine the sword’s overall weight
  • A damage component that influences how practical of a weapon the sword is

What Is Considered the System?

Systems iterate components to perform low-level functions like performing physics calculations or rendering graphics. Systems provide global scope, services, and management for component classes. It’s basically the logic that operates on the components.

For example, a camera system manages every entity with a camera component, controlling which camera is active.

What’s a Composition?

Compositions allow developers to attach more components to add additional functionalities, behavior, or appearance.

The Advantages of ECS

Here’s why Entity Component Systems are a benefit to programmers:

  • Programmers can use ECS to create shorter, less complicated code
  • It offers a clean design that employs decoupling, encapsulation, modularization, and reusability methods
  • It lets programmers mix reusable parts, providing better flexibility when defining objects
  • It features very flexible emergent behavior
  • It offers an architecture for both 3D and VR development, allowing you to build the latter application in terms of complexity
  • It allows non-techies to script by behavior
  • It’s an easy choice for unit testing and mocking
  • You can switch components with mocked components at run-time
  • It helps you to add or bolster new features
  • It’s a friendly method for multi-threading and parallel processing
  • It helps programmers separate data from the functions that can act on it.

The Disadvantages of Using ECS

Of course, every tool has its downside. Here’s are things about ECS that aren’t as great:

  • ECS is not very well known. Most people haven’t even heard of it. That can pose problems for collaboration.
  • It’s not as concretely defined as other patterns, like Model-View-Controller (MVC).
  • It’s challenging to apply correctly, and it’s easy to misuse. As a result, programmers need to think more about how to design good components.
  • ECS requires programmers to write many small systems which can potentially be used in vast numbers of entities. This method results in a risk of writing very inefficient code

Learn more about coding concepts such s components with our Caltech Coding Bootcamp

Entity Component System Example

This illustration, provided by Docs.unity3d, shows an example of ECS architecture and how the parts all work together.

Entity_Component_System

Data flow in ECS

We can break down the ECS data flow into the following steps:

  • System: The system listens to outside events and publishes updates to the components.
  • Component: The components listen to system events, then update their state.
  • Entity: The entity gains behavior through the changes in component states.

So, a gamer presses the “right arrow” key while adventuring in a fantasy world. The player input system detects the gamer’s key press and updates the motion component. The motion system activates and “sees” that the entity’s motion is to the right, so it applies the Physics force accordingly. Then, the render system takes over and reads the entity’s current position, drawing it according to its new spatial definition.

Why Is ECS Used?

We’ve already touched upon the advantages of ECS, but there are four reasons why game developers like using it:

  • First, it can support many game objects
  • Second, its code is more reusable
  • Third, it allows a more dynamic coding style
  • Fourth, it lets developers extend/add new features

How Is ECS Different From OOP?

Some people consider ECS an alternative to Object Oriented Programming, otherwise known as OOP. Although the two share some overlapping similarities, there are four key differences between them:

  • OOP encourages data encapsulation while ECS promotes exposed plain old data (POD) objects
  • OOP considers inheritance a first-class citizen, while ECS considers composition a first class
  • OOP colocates data with behavior, but ECS separates the data from behavior
  • OOP Object instances are single static, and entities may dynamically change multiple components

How Is ECS Different From Entity-Component Frameworks?

Although they share two-thirds of the same name, ECS and Entity-Component Frameworks aren’t the same. EC frameworks, like the ones usually found in game engines, are like ECS in that they allow entity creation and component composition.

But in EC, components are classes that have both data and behavior. Also, behavior is executed directly on the component. Here’s what a simple EC framework looks like, courtesy of GitHub:

class IComponent {

public:

virtual void update() = 0;

};

class Entity {

vector<IComponent*> components;

public:

void addComponent(IComponent *component);

void removeComponent(IComponent *component);

void updateComponents();

}; 

Is ECS a Lower Level of Abstraction?

Not really. Although several ECS designs may leverage low-level machine optimizations, an ECS’s written code doesn’t have to be lower or higher than other approaches.

Does ECS Require Writing More Code?

The answer really depends on the ECS framework and what kind of engine you’re using. When you integrate an ECS framework with an engine, you can get pretty compact and concise code that’s sometimes shorter than the non-ECS alternatives.

But when your ECS isn’t integrated with an engine, you will need additional glue-code to make a bridge between the native engine types and the ECS. This procedure can cause an application to write more code.

In the final analysis, though, any time spent writing ECS code is typically offset by the time savings you get from having a more maintainable codebase.

Is ECS Fast?

As a rule, yes, although it depends on what's being measured and the ECS implementation itself because different implementations result in different tradeoffs. So, for example, an operation that's slow in one framework could be extremely fast in another.

In terms of speed, ECS implementations are typically good at dynamically changing components at run-time and linearly querying and iterating entity sets. On the other hand, ECS implementations fall short in speed when executing queries or operations that need highly specialized data structures like spatial structures and binary trees.

But you can get the most out of your ECS if you familiarize yourself with the implementation’s tradeoffs and leverage its design.

Is ECS Code More Reusable?

Yes, because an ECS’s behaviors are matched with a set of components, not tightly coupled with a class like in OOP. Since behaviors aren’t tied to one class, they can be used across different classes of entities.

Furthermore, programmers can introduce new systems at any stage of development. The systems will get automatically matched with any new or existing entities that contain the right components.

Is ECS Good for Multi-threading?

Usually, yes, because the separation of data and behavior means it’s easier to identify individual systems, their dependencies, and how the developer should schedule them.

Can ECS Be Used Outside of Gaming?

Yes, it can and has been used in non-gaming projects.

How Do You Create a Hierarchy in ECS?

Here’s an approach you can use to create a hierarchy in an Entity Component System, coding courtesy of Github.

// Store the parent entity on child entities

struct Parent {

entity parent;

}; 

// Store all children of a parent in a component with a vector

struct Children {

vector<entity> children;

}; 

// Store children in linked list

struct ChildList {

entity first_child; // First child of entity

entity prev_sibling; // Previous sibling

entity next_sibling; // Next sibling

}; 

How Do You Store Spatial Data in ECS?

Spatial data structures use layouts that don’t match up well with the typical ECS layout. However, you can create a query that iterates the relevant entries, storing them in spatial structures at either the start or end of each frame.

Summarize and Review

Here are the salient takeaways:

  • Entity-Component–Systems (ECS) is an architectural pattern
  • An entity is a distinct object representing an actor in a simulated space
  • A component is defined as a singular behavior that’s ascribed to an entity
  • A system iterates many components so they can perform low-level functions like pathfinding, rendering graphics, or performing physics calculations
  • A composition is an element to which programmers can attach more components, adding behaviors, additional appearance, or functionality
  • You can use an ECS pattern to create shorter, less complicated code
  • ECS isn’t as concretely defined as other patterns like MVC

Do You Want to Learn Coding?

If you want to get involved in a career that provides ample rewards and long-term job security, today's commercial sector is experiencing continued demand for more full stack developers.

So, if you want a new career in an exciting field with lots of growth potential or are just looking for a convenient and effective way to upskill, visit Simplilearn and see how their courses can open new doors. Check out Simplilearn today!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449

Learn from Industry Experts with free Masterclasses

  • From Concept to Code: Live Demo of a Food Delivery App Using MERN

    Software Development

    From Concept to Code: Live Demo of a Food Delivery App Using MERN

    1st Oct, Tuesday9:00 PM IST
  • Career Masterclass: MERN vs. Java Full Stack: Making the Right Career Move in 2024

    Software Development

    Career Masterclass: MERN vs. Java Full Stack: Making the Right Career Move in 2024

    23rd Jul, Tuesday7:00 PM IST
  • How To Become a Pro MERN Stack Developer in 2024

    Software Development

    How To Become a Pro MERN Stack Developer in 2024

    13th Jun, Thursday7:30 PM IST
prevNext