Entity Component System: Entity Component - RamilGauss/MMO-Framework GitHub Wiki
Create entity and get entity identity - eid.
auto eid = entityManager.CreateEntity();
Component c;
entityManager.SetComponent(eid, c);
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);
}
entityManager.RemoveComponent<Component>(eid);
auto hasComponent = entityManager.HasComponent<Component>(eid);
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
}
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);
TEntityList* entities = entityManager.GetByHas<TNameComponent>();