Filters
A filter is a cached query: the set of entities that have every Inc type, none of the Exc types and, if Any types are given, at least one of them. Filters are reactive: the world updates them on every structural change, so iterating one never scans the world.
Building a filter
var filter = world.Filter()
.Inc<Position>()
.Inc<Velocity>()
.Exc<Frozen>()
.End();
var positions = world.Pool<Position>();
foreach (int e in filter) {
ref var pos = ref positions.Get(e);
world.DestroyEntity(world.GetEntity(e)); // OK: current entity
}Identical filter constraints return the same Filter instance. Build filters in Init, not in Run: Filter() allocates a builder.
End() throws InvalidOperationException:
- for filters without a single
Inc<T>orAny<T>(exclude-only and empty filters are not supported); - when the same component type is in both
Inc<T>andExc<T>, or in bothAny<T>andExc<T>.
Any
Any<T> matches entities that have at least one of the listed types, on top of Inc/Exc:
// Position and (Health or Shield), not Frozen
var damageable = world.Filter()
.Inc<Position>()
.Any<Health>().Any<Shield>()
.Exc<Frozen>()
.End();Static specs
Filters like these as one-liners, with the constraints in the type:
_moving = world.Filter<Inc<Position, Velocity>, Exc<Frozen>>();
_notFrozen = world.Filter<Inc<Position>, Exc<Frozen>>();
_targets = world.Filter<Inc<Position>, Exc<Frozen>, Any<Health, Shield>>();Inc takes 1-6 types, Exc 1-4, Any 2-4; None fills an unused slot. Specs and builder filters deduplicate against each other.
Helpers
int count = filter.Count; // matching entities
bool empty = filter.IsEmpty;
bool contains = filter.Contains(entityIndex);
int first = filter.First(); // throws when empty
bool found = filter.TryGetFirst(out int e); // false when empty
int single = filter.Single(); // throws unless exactly one match
ReadOnlySpan<int> all = filter.Entities; // valid until the next structural changeFirst, TryGetFirst, Single and Entities give int slot indices; convert with world.GetEntity(e) before storing anything (see Handles vs. indices).
Enter / leave events
class SpawnView : IFilterListener {
public void OnEntityAdded(Filter filter, int entityIndex) { /* spawn */ }
public void OnEntityRemoved(Filter filter, int entityIndex) { /* despawn */ }
}
class ViewSystem : IInitSystem {
public void Init(World world, SharedData shared) {
var viewRequests = world.Filter<Inc<ViewRequest>>();
viewRequests.AddListener(new SpawnView());
}
}Callbacks run synchronously inside the structural change; the entity is alive on enter and may already be dying on leave. The one exception on enter is a component that a listener adds to an entity whose DestroyEntity is running: the dying entity (IsAlive false) enters the filters it then matches, with the components the destroy has not removed yet, and leaves them when the destroy removes that component too. If your listeners re-add components to dying entities, check world.IsAlive in OnEntityAdded. RemoveListener unregisters a listener.
A listener may undo the change it is told about: remove a component (or destroy the entity) in OnEntityAdded, add one back in OnEntityRemoved. Every listener hears an entity enter and leave alternately, in the order of the changes, and the last call it got matches what the filter holds. The nested change (also one made further in, from a callback of another entity's change) is told only to the listeners that heard of the change it undoes, and the listeners after the one that undid it hear of neither. A listener hears the changes that happen after it subscribed: one subscribed during a notification is not told of that change, but of the changes after it, nested ones included. One that an earlier listener unsubscribes is still called for the change being told, but hears no change nested in it. If a listener throws, the exception leaves the Add or Remove after the world is consistent: the filters not updated yet are updated, without calling their listeners, and an entity that lost its last component is destroyed. The listeners not called yet are not called for that change.
Changed in 2.5.0
Up to 2.4.0 the listeners after the one that undid the change got the two calls in reverse order: when the first listener removed the component in OnEntityAdded, the second got OnEntityRemoved and then OnEntityAdded for an entity that was no longer in the filter.
Iteration contract
- Iteration order is unspecified and changes with structural modifications. Do not rely on it.
- Safe inside
foreach: destroying the current entity, adding/removing components on the current entity, creating new entities. New entities that match the filter are not visited in the current loop. foreachdoes not lock the filter. An exception inside the loop leaves the world consistent.
Known limitation
Destroying or removing components from a not-yet-visited entity may cause an already-visited entity to be visited again (swap-remove). Under KENSEI_DEBUG this throws at the moment it would happen, after the removal itself is complete (the world stays consistent; the loop must not go on). Defer such changes with a CommandBuffer.
For loops that need contiguous component data, see owning groups.
