ECS System
What's a system ?
A system is a piece of logic that processes entities with specific components.
Systems operate on entities that have the required components. For example:
A MovementSystem might process all entities with both PositionComponent and VelocityComponent to update their positions based on their velocities.
A RenderSystem would draw all entities that have a SpriteComponent.
How to write one ?
In our game Engine, we let possibility for developers and community to write their own components. Components are written in C++.
Tutorial:
Firstly, if you didn't already read the ECS Component section, please take a moment to read it.
In this tutorial, we'll create a movement system. For that, we need two component: a position component and a velocity component.
The system will update the position with the velocity.
Let's see how to implement that.
First, create a MovementSystem.hpp file. And include the following files:
#include "Server/include/ISystem.hpp"
#include "VelocityComponent.hpp"
#include "PositionComponent.hpp"ISystem: the interface class for system. To create a new system, it must inherit from this interface.
Velocity and Position: two components (see ECS Component to create a component)(This file already includes IComponent.hpp)
Now, let's create our system class. It must inherit from the ISystem class.
class MovementSystem : public ISystem {
};As the IComponent interface, the ISystem has a pure virtual method getType() to override:
class MovementSystem : public ISystem {
public:
const std::string &getType()
{
return _type;
}
prviate:
std::string _type = "Movement";
};Type must be unique, if another system in the ECS has the same type name, one of the two will be overridden.
Then, ISystem has a second method to override, it's the method to get the function system:
std::function<void(ECS::Registry& reg)> getFunction()
{
};This method take the Registry in parameter. Like this, we can simply create our system function.
The function will get the components Position and Velocity and add the Velocity to the Position of all entities.
void updatePosition(ECS::Registry& entityManager) {
// we get all positions and velocities in the registry
auto velocities = entityManager.get_components<IComponent>("Velocity");
auto positions = entityManager.get_components<IComponent>("Position");
// Then for each of positions and velocities...
for (std::size_t i = 0; i < velocities.size() && i < positions.size(); i++) {
// ... if they exist ...
if (!velocities[i] or !positions[i])
continue;
// ... we cast the IComponent into the right type ...
Velocity* velocity = dynamic_cast<Velocity*>(velocities[i].get());
Position* position = dynamic_cast<Position*>(positions[i].get());
// ... and update the Position of an entity by adding the velocity to the position
entityManager.add_component<IComponent>(i, std::make_shared<Position>(velocity->dx + position->x, velocity->dy + position->y),"Position");
}
};Finally, in our method getFunction() we want to return our system function:
std::function<void(ECS::Registry& reg)> getFunction()
{
return [this](ECS::Registry& reg) {
updatePosition(reg);
};
}Here we are ! We have our first system !
But that's not the end. How will the game Engine use our system ?
For that, we must create an entry point. When the system will load in the game Engine, it will call this entry point to get the System class.
For that create a cpp file named MovementSystem.cpp put this on it:
#include "MovementSystem.hpp"
extern "C" ISystem* loadSystemInstance() {
return new MovementSystem();
}This function will return our System class previously created.
Compile your System with this command in your terminal:
g++ -fPIC -shared -o theNameOfYourSystem.so yourFile.cppReplace 'theNameOfYourSystem' by the name you want to give to your System file, and 'yourFile.cpp' by the MovementSystem.cpp.
Finally, place your file .so in the Server/lib/systems/ folder. The system will be automatically loaded when the game Engine will be running.