Understanding OrFilters

Hi,

I want to retrieve all entities that contain component A and at least one of the components B and C.
It’s clear to me that I need an OrFilter to achieve this, but looking at the OrFilter constructors (or the factory methods) I don’t understand how to create one.

The OrFilter takes a class X (“type”) and a list of ComponentFilters Y (“operands”).
If an entity has a component X, it is immediately included in the result. Else each ComponentFilter in Y is checked (if Y is not null).

I tried using it like this:

ComponentFilter filter = OrFilter.create(ComponentB.class, OrFilter.create(ComponentC.class, null));
entitySet = entityData.getEntities(filter, ComponentA.class);

This does not work, because the OrFilter.create(…) method is defined as follows:

public static <T extends EntityComponent> OrFilter<T> create(Class<T> type, ComponentFilter<? extends T>[] operands) {
   ...
}

This means that all “type” classes in the “operands” filters must extends the initial “type” class.
Since entity components and inheritance don’t mix, I must be horribly misunderstanding something here. Can anybody please give me a brief example on how to use OrFilters?

You can’t. This is a sign that your components are split when they shouldn’t be or that you really have two separate entity sets.

Hard to say for sure without specifics.

OrFilter is for filtering on a single component type. Like, give me all entities with Position where Position.zone=1 or Position.zone=2.

Wow, that explains a lot… thank you.

When I first read the class name “OrFilter”, I assumed it would work that way. And then I started designing my code around this misconception.
Seems I’ve been off in the wrong direction for some time now. Thanks for putting me back on track.

At least now I know how to solve the “Position.zone=1 or Position.zone=2” problem :smile:

:smile:

If you ever need advice on design specifics, just post. We’ve had some really interesting discussions along those lines here and sometimes it helps me solve design stuff I haven’t encountered yet in my own games.

While I’m here, another thing to point out is that the filter will only filter on a component that you actually get as part of the entity. So if you only ask for ComponentA then you can only filter on ComponentA.