Skip to main content

examples/queries/
query_run.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
11#[derive(Debug, Component)]
12pub struct Velocity {
13    pub x: f32,
14    pub y: f32,
15}
16
17#[derive(Component)]
18pub struct Mass {
19    pub value: f32,
20}
21
22fn main() {
23    let world = World::new();
24
25    let query = world.new_query::<(&mut Position, &Velocity)>();
26
27    // Create a few test entities for a Position, Velocity query
28    world
29        .entity_named("e1")
30        .set(Position { x: 10.0, y: 20.0 })
31        .set(Velocity { x: 1.0, y: 2.0 });
32
33    world
34        .entity_named("e2")
35        .set(Position { x: 10.0, y: 20.0 })
36        .set(Velocity { x: 3.0, y: 4.0 });
37
38    world
39        .entity_named("e3")
40        .set(Position { x: 10.0, y: 20.0 })
41        .set(Velocity { x: 4.0, y: 5.0 })
42        .set(Mass { value: 50.0 });
43
44    // The run() function provides a flecs::iter object which contains all sorts
45    // of information on the entities currently being iterated.
46    query.run(|mut it| {
47        while it.next() {
48            let mut position = it.field_mut::<Position>(0);
49            let velocity = it.field::<Velocity>(1);
50
51            println!();
52            // Print the table & number of entities matched in current callback
53            println!("Table: {:?}", it.archetype());
54            println!(" - number of entities: {}", it.count());
55            println!();
56
57            // Print information about the components being matched
58            for i in 0..it.field_count() {
59                println!(" - term {i} : ");
60                println!("   - component: {}", it.id(i).to_str());
61                println!("   - type size: {}", it.size(i));
62            }
63
64            println!();
65
66            for i in it.iter() {
67                position[i].x += velocity[i].x;
68                position[i].y += velocity[i].y;
69                println!(" - entity {}: has {:?}", it.entity(i).name(), position[i]);
70            }
71
72            println!();
73        }
74    });
75
76    // Output:
77    //  Table: Position, Velocity, (Identifier,Name)
78    //  - number of entities: 2
79    //
80    //  - term 1 :
81    //    - component: Position
82    //    - type size: 8
83    //  - term 2 :
84    //    - component: Velocity
85    //    - type size: 8
86    //
87    //  - entity e1: has Position { x: 11.0, y: 22.0 }
88    //  - entity e2: has Position { x: 13.0, y: 24.0 }
89    //
90    //
91    // Table: Position, Velocity, Mass, (Identifier,Name)
92    //  - number of entities: 1
93    //
94    //  - term 1 :
95    //    - component: Position
96    //    - type size: 8
97    //  - term 2 :
98    //    - component: Velocity
99    //    - type size: 8
100    //
101    //  - entity e3: has Position { x: 14.0, y: 25.0 }
102    //
103}
104
105#[cfg(feature = "flecs_nightly_tests")]
106#[test]
107fn test() {
108    let output_capture = OutputCapture::capture().unwrap();
109    main();
110    output_capture.test("query_run_iter".to_string());
111}