Entity-Component–System (ECS) is an architectural pattern. This pattern is widely used in game application development. ECS follows the composition over the inheritance principle, which offers better flexibility and helps you to identify entities where all objects in a game’s scene are considered an entity.
ECS programming is a method of writing code that provides high performance by default.
This system consists of three terms.
Let us learn this concept in detail:
In this Entity-Component System tutorial, you will learn:
- What is Entity-Component-System?
- What is an Entity?
- What is a Component?
- What is the System?
- Advantage of ECS
- Disadvantages of using ECS
- Entity Component System Example
- Data flow in ECS:
An entity is a distinct object representing an actor in a simulated space. It has not actual data or behavior. This Component gives an entity its data. It is typically a strut or dictionary.
For example, if we are simulating a game’s universe Minecraft, all the visible, tangible ‘things’ in the game universe like are counted in entities.
A Component is a singular behavior ascribed to an entity. The name of an element should ideally communicate what behavior the entity will exhibit. However, the component itself has no behavior. They are reusable modules that are attached to entities. The component provides appearance, behavior, and functionality.
All logic is implemented using components which helps you to define different types of objects by mixing and configuring components.
A system will iterate many components to perform low-level
functions such as rendering graphics, performing physics
calculations or pathfinding. It offers global scope, management, and services for classes of components.
However, systems are not optional, but we can use them to separate logic and data. It also helps you handle the logical components, act as data containers.
- Gravity (Velocity) – It helps you to accelerate velocity due to gravity
- It adds velocity to position
- Sets a bot-controlled entity’s input according to an AI agent
- Render (Position, Sprite) – Draws sprites
- Player Control (Input Player) helps you set the player-controlled entity’s input according to a controller.
A composition is an element you could attach more components to add additional appearance, behaviour, or functionality. You can also update the component values to configure the entity.
Here are advantages of using ECS in programming:
- You can create shorter and less complicated code.
- Very flexible Emergent behaviour.
- It provides an architecture for 3D and VR development.
- Enables scripting by behavior by non-techies.
- It helps you to divide your monolithic classes.
- Easy refuse and compossibility.
- An easy option for unit testing and mocking.
- It allows you to build a VR application in terms of complexity.
- Substitute components with mocked components at runtime.
- It helps you to extend new features.
- ECS is a friendly method for parallel-processing and multi-threading,
- Helps you to separate data from functions that act on it
- Provide better flexibility when defining objects by mixing with reusable parts.
- It offers a clean design using the decoupling, encapsulation, modularization, reusability methods.
Here, are some drawback of using ECS
- Not as concretely defined as other patterns such as MVC.
- Difficult to apply correctly, easy to misuse Good components require more thinking about design
- ECS requires writing many small systems which alliterate on potentially huge numbers of entities, which develop the risk of writing very inefficient code
- Most people have never even heard of this pattern.
Let us look at the example of a bunny:
See the dashed box around these components. That is the bunny entity – nothing than a container of components. You can also define entities as an aggregation of any subset of components.
You can see the above-given image. It is one of the bunny entities – nothing more than a container of components. You can define entities as an aggregation of any subset of components, like Carrot:
Systems behaviour is ECS model is change from one state to another.
Let us understand using this example:
Behaviors: “A bunny sitting on a tree falls due to gravity.”
How to Implement the above behavior?
You can make it by z value of more than 0 which decreases over time to 0.
Behavior: “Living beings age.”
Let see how you can implement this behavior?
You can make it so that Living Components have age value, which increases over time.
- Entity-Component–System (ECS) is an architectural pattern.
- An entity is a distinct object representing an actor in a simulated space.
- A Component is a singular behavior ascribed to an entity.
- A system will iterate many components to perform low-level functions such as rendering graphics, performing physics calculations or pathfinding.
- A composition is an element you could attach more components to add additional appearance, behaviour, or functionality.
- By using ECS pattern you can create shorter and less complicated code.
- It is not as concretely defined as other patterns such as MVC.
- Systems behaviour is ECS model is change from one state to another.