Skip to content

ECS Component

Thomaltarix edited this page Nov 3, 2024 · 3 revisions

ECS Component

What's a Component?

In our game engine, a component is a data structure that holds specific attributes related to an entity. Components are pure data, they do not contain any logic or behavior.

Examples of Components:

  • PositionComponent: Holds x, y coordinates (and possibly z for 3D).
  • VelocityComponent: Contains speed and direction.
  • HealthComponent: Keeps track of the entity's health points.
  • SpriteComponent: Holds information related to rendering, like texture or sprite sheet data.

How to Write a Component

In our game engine, we enable developers and the community to create their own components in C++. Here’s a tutorial to help you get started by creating a VelocityComponent.

Step 1: Create the Header File

First, create a new file named VelocityComponent.hpp. Include the IComponent header file located in the Engine/Shared/Interface/ folder:

#include "Engine/Shared/Interface/IComponent.hpp"

Step 2: Define Your Component Class

Create your component class and ensure it inherits from the AComponent class.

#include "Engine/Shared/Interface/IComponent.hpp"

class Velocity : public AComponent
{
    // Class content will go here
};

Step 3: Create a Constructor with the Data You Need

In a Velocity component, you likely need x and y values for speed. Define these values in the constructor.

#include "Engine/Shared/Interface/IComponent.hpp"

class Velocity : public AComponent
{
public:
    float x = 0.0f;  // Velocity x
    float y = 0.0f;  // Velocity y

    Velocity(float x = 0.0f, float y = 0.0f) : x(x), y(y) {} // Constructor for Velocity component
};

Now you've created your file, you'll be able to compile it in your systems. It's up to the systems to compile the components they need to use.

g++ -c yourFile.cpp
ar rvs youComponentName.a yourFile.o 

Finally, place your file .a in the Game/Engine/ folder. The components will be automatically linked when the game Engine will be running.

Clone this wiki locally