Entity Component System: Entity Component - RamilGauss/MMO-Framework GitHub Wiki

0. Prepare entity

Create entity and get entity identity - eid.

auto eid = entityManager.CreateEntity();

1. Adding

Component c;
entityManager.SetComponent(eid, c);

2. Modifying

2.1. Poor performance, but you can quickly implement the source code.
Use this approach in all cases: entity has no component or entity already has component.

Component c;
c.x = 42; // first copying
entityManager.SetComponent(eid, c);// second copying

2.2. High performance, but long entry in the source code.
Use this approach when entity already has component.

auto pC = entityManager.ViewComponent<Component>(eid);
if (pC != nullptr) {
    pC->x = 42;// Copy at once
    entityManager.UpdateComponent(eid, pC);
}

3. Removing

entityManager.RemoveComponent<Component>(eid);

4. Does it has?

auto hasComponent = entityManager.HasComponent<Component>(eid);

5. Get an entity by unique value of the component

struct TNameComponent : nsECSFramework::IComponent
{
    std::string name;
    bool IsLess(const IComponent* pOther) const override
    {
        return name < ((const TNameComponent*)(pOther))->name;
    }
    bool IsEqual(const IComponent* pOther) const override
    {
        return name == ((const TNameComponent*) (pOther))->name;
    }
};
TNameComponent nameC;
nameC.name = "some str";
auto entityId = entityManager.GetByUnique<TNameComponent>(nameC);
if (entityId != nsECSFramework::None) {
    // Do some
}

6. Get entities by value of the component

struct TNameComponent : nsECSFramework::IComponent
{
    std::string name;
    bool IsLess(const IComponent* pOther) const override
    {
        return name < ((const TNameComponent*)(pOther))->name;
    }
    bool IsEqual(const IComponent* pOther) const override
    {
        return name == ((const TNameComponent*) (pOther))->name;
    }
};
TNameComponent nameC;
nameC.name = "some str";
TEntityList* entities = entityManager.GetByValue<TNameComponent>(nameC);

7. Get all entities that has the component

TEntityList* entities = entityManager.GetByHas<TNameComponent>();
⚠️ **GitHub.com Fallback** ⚠️