libgdx / ashley

A Java entity system inspired by Ash & Artemis.
Apache License 2.0
875 stars 144 forks source link

Is it possible to get entities by their common interface? #268

Closed xahon closed 6 years ago

xahon commented 6 years ago
interface IMyComponentBase implements Component {}

class Derived1 implements IMyComponentBase {}
class Derived2 implements IMyComponentBase {}

engine.getEntitiesFor(
    Family.all(IMyComponentBase.class).get() // not working
);
aesophor commented 6 years ago

I have this problem too, It only returns the first entity that matches the specified family.

mgsx-dev commented 6 years ago

@xahon you can't do that, components are registered by its concrete class. and what you want to do is against ECS principle IMO.

xahon commented 6 years ago

@mgsx-dev may be you're right. I still haven't stopped thinking in OOP style but right now I can't give you an example for this problem

metaphore commented 6 years ago

I'm a bit late to the party, but maybe you will find it useful. Family is a powerful tool, you still can put all required component classes manually under any rule and get pretty the same result. If you prefer, a bit of automation could be achieved with reflection.