top of page
Search
chilaganirajesh95

Entity-Component System (Engine System Proposal)

In this blog, I will discuss the Engine System that I will be adding to the current game engine project. I will be creating an Entity-Component(can also be called a Component/Entity) System that can be used to create and manage game objects in the game. So what is an Entity-Component system? It is an architectural pattern used in game development to create and manage game objects in a game. It follows the composition over inheritance principle and allows flexibility in defining entities(also called game objects). You can create components that contain data and attach these components to entities to make it a more meaningful object.

For example, you can create a base entity using the interface provided by the ECS and create your own components and attach it to the entity. So if I need to create an entity that has some positional information I will create a Transform Component as below and add to the entity using the interface provided by the system. This approach makes it very easy to add or remove components to an entity as per your wish.

struct TransformComponent
{
    Vector Position;
    Vector Rotation;
    Vector Scale;
}

ECS also helps in programming a specific behavior based on components for example let's say you have a physics component you can now run physics updates on all entities that have a physics component. Similarly, you can render all entities that have Mesh component and so on. ECS makes it easy to add custom behavior to a game object and gives full control over the customization. It is very simple to add or remove features using ECS.


Entity-Component System Architecture & Features

Entity: It is a container into which components can be added to make it a meaningful object and program some behavior.

Component: An object with specific data that is used to represent some information or program-specific behavior based on the data.

In this system, the Entity is a class with the following methods

AddComponent, RemoveComponent, GetComponent, HasComponent() and more.

Components are basically structs that users will create to attach to entities. Components or their types need not be registered with ECS to be attached to the entity.


Some of the features provided by ECS API are

- Interface for Create/Destroy an Entity

- Adding Components to Entity

- Removing Components from Entity

- Get Component from Entity

- Methods to perform validations like HasComponent, IsValid.

- Views to iterate all entities based on components


A View is basically a collection of entities so that you can iterate on them to perform some operation. Views are created based on components you have to provide a component to get a view. Only entities that contain the specified component are included in the collection.

Following is an example of a view

auto TransformView = GetView<TransformComponent>();

for(auto entity : TransformView)

{

UpdateTransform(entity.GetComponent<TransformComponent>); }


All the component's data can be stored in files and can be loaded when required. This system provides a way to store components data for the predefined components in files but the user has complete freedom to make their own way to store the custom components in files or use the existing approach. JSON/Lua will be used to store the component data in files.

Following are examples for the file representation of components data in Lua & JSON

JSON
 "Components": 
[
 {
  "Name": "TransformComponent",
  "Position": { "X":100, "Y":100,}
  "Rotation": { "X":0, "Y":0,}
 }
]
Lua
{
TransformComponent = {
Position={100,100},
Rotation={0,0}
  }
}
 

Note: Either Lua or JSON will be used in the actual implementation the examples are for users who want to make their own way to store components data in files for the custom components.

Following is an example code which describes the usage of this system

//Create some components
struct TagComponent
{
    string Name;
    TagComponent()=default;
    TagComponent(const char* i_Name):Name(i_Name){}
}
struct TransformComponent
{
    Vector Position;
    Vector Rotation;
    Vector Scale;
    TransformComponent()=deafult;
    TransformComponent(const Vector& Pos,const Vector& Rot,const Vector& Scale)
}
//Create Entity
Entity E1 = CreateEntity();
E1.AddComponent<TagComponent>("E1 Object");
E1.AddComponent<TransformComponent>();

//Get Component
TagComponent& T = E1.GetComponent<TagComponent>();
T.Name="Updated the Name";

Some of the challenges I expect working on this system are implementing the view system

I think making an efficient view system might be difficult and it is a stretch goal for me. If I have time I will also try to extend the view system to multicomponent views where you can get objects with multiple components.

18 views0 comments

Recent Posts

See All

Commentaires


Post: Blog2_Post
bottom of page