macro_rules! ecs_find {
(...) => { ... };
}Expand description
Finds a single entity in an ECS world and performs an operation on it, if found.
ⓘ
ecs_find!(world, entity, |comp_a: &CompA, comp_b: &mut CompB, ...| { ... });The ecs_find! macro finds a single entity in an ECS world and performs an operation
on it, if that entity is found in archetype storage. It takes the following arguments:
world: The world (as an expression) that you want to query.entity: The entity handle you want to look up. May be anEntity<A>orEntityAny.|comp_a: &CompA, comp_b: &mut CompB, ...| { ... }: A closure containing the operation to perform on the current entity’s data. The parameters of the closure determine what components for the entity that this query will access and how. Any component can be accessed as&Componentor&mut Component. The query will only check archetypes that are known at compile-time to have all components requested in the query closure.- Note that this closure is always treated as a
&mut FnMut.
- Note that this closure is always treated as a
The ecs_find! macro returns true if the entity was found, or false otherwise.
Special Arguments
Query closure arguments can have the following special types:
&Entity<A>/&EntityAny: Returns the current entity being accessed by the closure. This is somewhat redundant forecs_find!queries, but useful forecs_iter!loops. Note that this is always read-only – the entity can never be accessed mutably.&Entity<_>: When used with the special_wildcard, each execution of this query will return a typedEntity<A>handle for the exact archetype matched for this specific execution. This can be used to optimize switched behavior by type.&OneOf<A, B, ...>or&mut OneOf<A, B, ...>: SeeOneOf.
In query closures, a special MatchedArchetype type alias is set to the currently
matched archetype being accessed during this execution of the closure. This can be used
for generic operations.
Example
use gecs::prelude::*;
pub struct CompA(pub u32);
pub struct CompB(pub u32);
pub struct CompC(pub u32);
ecs_world! {
ecs_archetype!(ArchFoo, 100, CompA, CompB);
ecs_archetype!(ArchBar, 100, CompA, CompC);
}
// If you need to use a non-mut reference, see the ecs_find_borrow! macro.
fn add_three(world: &mut World, entity: Entity<ArchFoo>) -> bool {
// The result will be true if the entity was found and operated on.
ecs_find!(world, entity, |comp_a: &mut CompA| { comp_a.0 += 3; })
}
fn add_three_any(world: &mut World, entity: EntityAny) -> bool {
// The query syntax is the same for both Entity<A> and EntityAny.
ecs_find!(world, entity, |comp_a: &mut CompA| { comp_a.0 += 3; })
}
fn main() {
let mut world = World::default();
let entity_a = world.push::<ArchFoo>((CompA(0), CompB(0)));
let entity_b = world.push::<ArchBar>((CompA(0), CompC(0)));
assert!(ecs_find!(world, entity_a, |c: &CompA| assert_eq!(c.0, 0)));
assert!(ecs_find!(world, entity_b, |c: &CompA| assert_eq!(c.0, 0)));
assert!(add_three(&mut world, entity_a));
assert!(add_three_any(&mut world, entity_b.into())); // Convert to an EntityAny
assert!(ecs_find!(world, entity_a, |c: &CompA| assert_eq!(c.0, 3)));
assert!(ecs_find!(world, entity_b, |c: &CompA| assert_eq!(c.0, 3)));
}