Filter Help

I’m trying to do maybe a little bit of an advanced filter for searching for entities.

I have a component called “Position” (has variables float x, and float z) and a component called “Crate” (has variable heldByEntityId)

When the user clicks a button they are supposed to pick up the crate that is sitting next to their character. I need a way to search my entity data for entities that have both Position and Crate, and “Crate” needs to have heldByEntityId == null and “Position” needs to be within a certain radius of the player.

to filter for “Crate” is pretty staight forward. i found an example using FieldFilter to check for a value in the component. What I’m not so sure about is how to check for position because it needs to be within a range.

Is there a filter for doing this? would I need to make my own? if I have to make my own what is the general way to go about doing it?

You will also run into an issue where you can’t filter on more than one component type at a time, anyway.

First, it seems odd to have a “crate” component… even stranger that it also somehow contains who is holding it. I would have thought that these concepts were separate components as many different things can conceptually be held and they are probably not all crates. For example, I might have a separate HeldBy component… and I’d probably make Position and HeldBy mutually exclusive. But maybe your game is only players and crates and therefore “heldby” and “crate” are 100% synonymous but then you also see how your objects are forced to have a world position even when their position is dictated by something else’s position. Still, I suppose you can handle that in a system also… just keep moving its position to be its holder’s.

There isn’t currently a built in range filter though you are welcome to implement whatever custom filters you like… they just won’t be optimized in the database layer if you are using persistence. (Depending on how many crates there are, it may not matter, though.) If you are not using persistence then there is no performance difference between using the stock filters and using your own, they are still going to iterate over every component and call evaluate().

In fact, in that case a custom filter could be more performant since you wouldn’t have all of the (admittedly small) AndFilter overhead.

public class PositionFilter implements ComponentFilter<Position> {
private Vector3f center;
private float radius;
public PositionFilter( Vector3f center, float radius ) { = center;
this.radius = radius;

public Class&lt;Position&gt; getComponentType() {
    return Position.class;

public boolean evaluate( Position pos ) {
   //check pos against center and radius


Then just get all of the nearby crates and ignore the ones that are already held. (Note: if heldby and position were mutually exclusive then you’d only be getting non-held crates in the first place.)

Edit: fixed the type brackets in the code examples because our forum for developers ironically doesn’t show code blocks properly.

1 Like

ah I thought the filter parameter had a “…” on it for many filters. I’m mistaken.

anyway my game isn’t really a “game”, i got sidetracked reading about entity systems and I wanted to make something to kind of learn about them. I’m not using a database or anything so I’m not concerned about anything related to that. You’re probably right about “crate” not being the best name, at the time i think i was likely thinking “what should be the name of the component that i add to this crate entity?” =D . currently the game only has crates anyway.

I don’t think position and Heldby/crate could be mutually exclusive. because everything has a position. For instance the player has a position so it would in theory be possible for the player to pick himself up and defy at least one law of physics (without further qualifiers in the entity data).

My thinking was that the “Crate” component would be what determines that its an entity that can be picked up, (and the isHeldBy variable in it determines if its just sitting on the floor or if someone is holding it). I guess I could have a 3rd component called “Interactable” or something. and then Crate/HeldBy would only be added once its actually being held. Either way I think I’m in a situation where I need to find an entity based on two criteria in different components so its 6 egg or a half dozen situation.

I’m going to try with this position filter tomorrow, i dont see any reason it would work as long as I add the extra search for the Crate component.

thanks for your help