Skip to content

SFME::ECS

Sztergbaum Roman edited this page Feb 17, 2018 · 73 revisions

Welcome to the page of the SFME::ECS module

Modules contents

A complete example can be found here

Include hierarchy can be found here


How works the System

SFME have 3 differents kinds of systems:

  • PreUpdate: These systems are the first to be updated in the game loop, they are generally used to retrieve user input, or manage network events for example.
  • LogicUpdate: These systems are the second to be updated in the game loop, they are generally used for game logic such as movement or collisions for example.
  • PostUpdate: These systems are the last to be updated in the game loop, they are generally used for rendering or interpolation for example.

The pseudo code will look like this :

    while(running) {
            updateSystems(SystemType::PreUpdate);
            _timeStep.startFrame();
            while (_timeStep.isUpdateRequired()) {
                updateSystems(SystemType::LogicUpdate);
                _timeStep.performUpdate();
            }
            updateSystems(SystemType::PostUpdate);
    }

This game loop is based on the gafferon on games tutorial: Fix your timestep.


This class will manage the systems of the entity component system. You will be able to add, remove, retrieve or delete systems through it.

The class is templated on the entity manager and she is a receiver.

Class Signature

template <typename EntityManager>
class SystemManager final : public sfme::mediator::Receiver<SystemManager<EntityManager>>;

Constructor

SystemManager(sfme::mediator::EventManager &evtMgr, EntityManager &ettMgr) noexcept;
Parameters
  • evtMgr The event manager will be provided to the system when it is created.
  • ettMgr The entity manager will be provided to the system when it is created.

Member function : update

void update() noexcept;
Notes

This is the function that will update your systems. Based on the logic of the different kinds of SFME systems, this function will take care of updating your systems in the right order. If you decide to mark a system, it will be automatically deleted at the next loop turn through this function. The function will also release the entities marked through the function of the entity manager: sweepEntities.

Click here for jump to example.

Member function: createSystem

template <typename System, typename ...Args>
System &createSystem(Args &&...args) noexcept;
Template parameters
  • System Represents the type of system you want to load.
  • Args Represents the arguments possibly necessary to the creation of the system.
Return value

Returns a reference to the loaded System.

Note

If the system is already loaded, it will be returned to you. The EntityManager and the EventManager will be sent implicitly to the creation of each system.

Click here for jump to example.

Member function: enableSystem

template <typename System>
bool enableSystem() noexcept
Template parameters
  • System Represents the type of system you want to enable.
Return value
  • true Will return true if the system was successfully enabled.
  • false Will return false if the system was successfully disabled or the system does not exist.
Click here for jump to example.

Member function: disableSystem

template <typename System>
bool disableSystem() noexcept

Include Hierarchy


Examples

SystemManager


update. Click here for jump to the source code. Click here for jump back to the function documentation.

#include <SFME/ecs/ecs.hpp>
#include "ecs_common_example.hpp"

int main()
{
    //! Managers
    sfme::mediator::EventManager evtMgr;
    sfme::example::EntityManager ettMgr;
    sfme::ecs::SystemManager<sfme::example::EntityManager> systemMgr{evtMgr, ettMgr};

    //! System Loading
    systemMgr.loadSystems<sfme::example::system::PostUpdate,
        sfme::example::system::PreUpdate,
        sfme::example::system::Logic>();

    //! Game Loop
    for (int i = 0; i < 50000; ++i) {
        systemMgr.update();
    }
    return 0;
}

loadPlugin/loadPlugins/createSystem/loadSystems. Click here for jump to the source code. Click here for jump back to the function documentation.

#include "ecs_common_example.hpp"

namespace fs = std::experimental::filesystem;

int main()
{
    sfme::mediator::EventManager evtMgr;
    sfme::example::EntityManager ettMgr;
    sfme::ecs::SystemManager<sfme::example::EntityManager> systemMgr{evtMgr, ettMgr,
                                                                     fs::current_path() / "example/systems"};

    //! Simple system creation
    systemMgr.createSystem<sfme::example::system::Logic>();

    //! Simple system creation with user data.
    systemMgr.createSystem<sfme::example::system::WithUserData>(1, 2);

    //! Multiple systems creation
    systemMgr.loadSystems<sfme::example::system::PostUpdate, sfme::example::system::PreUpdate>();

    //! Simple plugged system creation
    systemMgr.loadPlugin("sfme_ecs_foo_plugin_example");

    //! Multiple plugged system creation
    systemMgr.loadPlugins();
    return 0;
}

Expected output:

1 2
sfme::example::plugins::FooSystem::createSystem
1518859634113 [system_manager] debug: sfme_ecs_foo_plugin_example has been successfully loaded.
sfme::example::plugins::BarSystem::createSystem
1518859634124 [system_manager] debug: sfme_ecs_bar_plugin_example.dll has been successfully loaded.
sfme::example::plugins::FooSystem::createSystem
1518859634135 [system_manager] info: sfme_ecs_foo_plugin_example.dll system already loaded

enableSystem/disableSystem/enableSystems/disableSystems. Click here for jump to the source code. Click here for jump back to the function documentation.

#include "ecs_common_example.hpp"

int main()
{
    sfme::mediator::EventManager evtMgr;
    sfme::example::EntityManager ettMgr;
    sfme::ecs::SystemManager<sfme::example::EntityManager> systemMgr{evtMgr, ettMgr};

    systemMgr.loadSystems<sfme::example::system::PostUpdate, sfme::example::system::Logic,
        sfme::example::system::PreUpdate>();
    systemMgr.update();
    systemMgr.disableSystem<sfme::example::system::PreUpdate>();
    systemMgr.update();
    systemMgr.enableSystem<sfme::example::system::PreUpdate>();
    systemMgr.update();
    systemMgr.disableSystems<sfme::example::system::PreUpdate, sfme::example::system::PostUpdate>();
    systemMgr.update();
    systemMgr.enableSystems<sfme::example::system::PreUpdate, sfme::example::system::PostUpdate>();
    systemMgr.update();
    return 0;
}

Expected output:

virtual void sfme::example::system::PreUpdate::update()
virtual void sfme::example::system::PostUpdate::update()
1518859548747 [system_manager] debug: PreUpdate has been disabled
virtual void sfme::example::system::PostUpdate::update()
1518859548748 [system_manager] debug: PreUpdate has been enabled
virtual void sfme::example::system::PreUpdate::update()
virtual void sfme::example::system::PostUpdate::update()
1518859548748 [system_manager] debug: PreUpdate has been disabled
1518859548748 [system_manager] debug: PostUpdate has been disabled
1518859548748 [system_manager] debug: PreUpdate has been enabled
1518859548748 [system_manager] debug: PostUpdate has been enabled
virtual void sfme::example::system::PreUpdate::update()
virtual void sfme::example::system::PostUpdate::update()