Skip to content

Registry

A Registry is the core class of the ECS, which stores the differents SparseArrays in an unordered_map, using the type of the Component stored in the SparseArray as the index in the associative array.

When a SparseArray is created, it creates multiple functions: - a function that allows us to delete the Component of a given Entity (stored in _erase_functions). - a function that allows us to add a new Entity to the array (stored in _add_functions).

There is also the Entity management part in the Registry, to create, delete and update the differents Components of the Entity.

The Registry class looks like this:

class Registry
{
public:
    Registry(){};

    template <class Component>
    sparse_array<Component> &register_component();

    template <class Component>
    sparse_array<Component> &get_components();

    template <class Component>
    sparse_array<Component> const &get_components() const;

    // #########################################################
    // #                 ENTITY MANAGEMENT                     #
    // #########################################################

    Entity spawn_entity();
    void kill_entity(Entity const &e)

    template <typename Component>
    typename sparse_array<Component>::reference_type add_component(Entity const &to, Component &&c);

    template <typename Component, typename... Params>
    typename sparse_array<Component>::reference_type emplace_component(Entity const &to, Params &&...p);

    template <typename Component>
    void remove_component(Entity const &from);

private:
    std::unordered_map<std::type_index, std::any> _components_arrays;
    std::unordered_map<std::type_index, std::function<void(Registry &, Entity const &)>> _erase_functions;
    std::unordered_map<std::type_index, std::function<void(Registry &)>> _add_functions;
    std::list<int> _empty_entities;
};

Constructor

Registry()

Registry()
Default constructor of Registry

Functions

register_components()

template <class Component>
sparse_array<Component> &register_component();
Creates and adds a SparseArray using the given Component in the Registry, and returns it.

Usage

SparseArray velocity = Registry.register_component<Velocity>();
Here the Velocity parameter is a Component.

get_components()

template <class Component>
sparse_array<Component> &get_components();
Returns the SparseArray of the given Component.

get_components()

template <class Component>
sparse_array<Component> const &get_components() const;
Same but returns a const SparseArray of the given Component.

Entity management

Functions

spawn_entity()

Entity spawn_entity();
Creates a new Entity, by using the index of a previously deleted Entity (using _empty_entities), or by calling _add_functions on every SparseArray existing, and returns an Entity containing the index.

kill_entity()

void kill_entity(Entity const &e);
Deletes the Entity, by calling _delete_functions on every SparseArray, and adds the index of the newly deleted Entity in _empty_entities.

add_component()

template <typename Component>
typename sparse_array<Component>::reference_type add_component(Entity const &to, Component &&c);
Adds a value at the given index (Entity) to the SparseArray corresponding to the given Component.

emplace_component()

template <typename Component, typename... Params>
typename sparse_array<Component>::reference_type emplace_component(Entity const &to, Params &&...p);
Same, but moves the value instead of copying it.

remove_component()

template <typename Component>
void remove_component(Entity const &from);
Removes the value at the given index (Entity) from the SparseArray corresponding to the given Component;

Values

_components_arrays

std::unordered_map<std::type_index, std::any> _components_arrays;
Stores the different SparseArrays.

_erase_functions

std::unordered_map<std::type_index, std::function<void(Registry &, Entity const &)>> _erase_functions;
Stores the erase functions created when creating a new SparseArray, using the type of Component as the index.

_add_functions

std::unordered_map<std::type_index, std::function<void(Registry &)>> _add_functions;
Stores the add functions created when creating a new SparseArray, using the type of Component as the index.

_empty_entities

std::list<int> _empty_entities;
Stores the list of empty indexes, used to manage memory cache more efficiently