Skip to content

Filters

Sometimes, a dynamic on-the-fly filter is needed to process only a subset of Entities; this enables us to quickly adjust our ECS logic to do different subsets of work without requiring a growing amount of queries to be defined.

Streams are lightweight views over their Query, so filtering never mutates the original Stream nor the underlying Query – you simply get a new, narrower view to run.

Two Flavors of Filtering

FlavorGranularityHow it narrowsHonored by
Subset & Excludewhole Archetypesadditional Has/Not-style clauses that prune Archetypes before iterationeverything: For, Job, Raw, Blit, enumeration, Count
Whereindividual EntitiesLINQ-style lambda predicates on component values, evaluated live during the runFor and Job

Both compose freely: the Archetype filters prune entire tables up front, and the Where predicates skim the Entities that remain.

csharp
var stream = world.Stream<Position, Health>();

var wounded = (stream with { Exclude = [ Comp<Dead>.Plain ] })  // Subset & Exclude
    .Where((in Health h) => h.Current < h.Max);                 // Where (lambda)

Neofox: floof_mug Can I interest you in a Grape-Colored Example

There's an appetizer on this topic! Check out Thanos for an, ahem, "practical" example of using filters.