QueryPredicate
An object that defines the criteria for an entity query.
Declaration
struct QueryPredicate<Value>Mentioned in
Overview
Query predicates specify the entities an EntityQuery returns from a scene. Predicates describe entities based on which components they contain, or on the entity’s relationship to other entities in the scene. For example, you can build a predicate to retrieve the model entities from a scene.
let modelPredicate = QueryPredicate.has(ModelComponent.self)Create compound predicates
You can combine predicates using Swift’s logical operators to create compound predicates. QueryPredicate supports Swift’s logical AND (&&), logical OR (||), and logical NOT (!) operators. The following code shows how to build a compound predicate that returns all entities that are either model entities or anchor entities:
let orPredicate: QueryPredicate<Entity> =
.has(ModelComponent.self) || .has(AnchorComponent.self)Use parentheses to control the order of operations when combining predicates. For example, you can create a query that returns any entity that has both a model component and a physics body component, or any entity that has only an anchor component.
let multiPredicate: QueryPredicate<Entity> =
.has(ModelComponent.self) && .has(PhysicsBodyComponent.self) ||
.has(AnchorComponent.self)