Skip to main content

examples/queries/
query_find_entity.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5#[derive(Debug, Component)]
6pub struct Position {
7    pub x: f32,
8    pub y: f32,
9}
10
11fn main() {
12    let world = World::new();
13    // Create a few test entities for a Position query
14    world.entity_named("e1").set(Position { x: 10.0, y: 20.0 });
15
16    world.entity_named("e2").set(Position { x: 20.0, y: 30.0 });
17
18    // Create a simple query for component Position
19    let query = world.new_query::<&Position>();
20
21    let entity: Option<EntityView> = query.find(|pos| (pos.x - 20.0).abs() < f32::EPSILON);
22
23    if let Some(entity) = entity {
24        println!("Entity found: {:?}", entity.path().unwrap());
25    } else {
26        println!("Entity not found");
27    }
28
29    // Output:
30    //  Entity found: "::e2"
31}
32
33#[cfg(feature = "flecs_nightly_tests")]
34#[test]
35fn test() {
36    let output_capture = OutputCapture::capture().unwrap();
37    main();
38    output_capture.test("query_find_entity".to_string());
39}