alecthomas / entityx

EntityX - A fast, type-safe C++ Entity-Component system
MIT License
2.21k stars 295 forks source link

Grabbing access to the entity that owns a component? #242

Closed jasonmeverett closed 3 years ago

jasonmeverett commented 3 years ago

Say I have a component handle:

Entity * e1;
ComponentHandle<ComponentType1> handle = e1->component<ComponentType1>();

I'd like to get the orignal entity from the component handle, such as:

Entity * e2 = handle->GetOwningEntity();
e2 == e1

Is this possible in EntityX? A workaround I could think of is having:

struct ComponentType1 : public Component<ComponentType1>
{
    ComponentType1();
    ComponentType1(Entity * e) : self(e) {}

    Entity * self;
}

And then always calling such as:

Entity * e1;
ComponentHandle<ComponentType1> handle = e1->component<ComponentType1>(e1);

Is there a more structured way to do this?

Thanks in advance!

alecthomas commented 3 years ago

Can we move these discussions to https://discourse.swapoff.org/c/entityx/7? GitHub is not really a great conversation/help platform. If you'd like to move these questions over there, I'm happy to chat.