How to find an Entity which contain a known component?

I want to find an Entity which contain this component. Only one Entity is supposed to contain it.

public class MyComp implements EntityComponent{
    
}

I should do something like:

ed.findEntity(Filters.fieldEquals(MyComp.class, "???", null),MyComp.class);

Which earns me a

IllegalArgumentException: Field not found:???

If there is only ever one of them then you don’t need the filter.

Else, the component has no fields so there is nothing unique about it and you probably have a design issue.

What is this component used for?

I didn’t mean this. I meant that at a specific time there is only one Entity containing it; but the Entity is not the same from start to finish.

Yeah, I might keep a variable with a reference to the Entity when I attach the component to it, but I hoped on a more elegant solution.

In this case is a label for a special monster that must be treated differently from the others.

Maybe you should rather have a “WorldEntity” with a “SpecialMonster” component that contains the entity ID of the current special monster.

1 Like

But is it by the type of component or some field?

If it’s just by the type of component then you do not need to specify the filter. Just ask for “All entities with this componen type”… and you will just get one because only one entity has that type of component.

1 Like