examples/queries/
query_chaining_queries.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4// this example is to showcase how you can chain queries together where the second query
5// uses the results of the first query to query the results
6
7#[derive(Component)]
8struct Enchanted;
9
10#[derive(Component)]
11struct Location {
12    x: f32,
13    y: f32,
14}
15
16#[derive(Component)]
17struct Ability {
18    power: f32,
19}
20
21#[derive(Component)]
22struct ArtifactPower {
23    _magic_level: f32,
24}
25
26fn main() {
27    let forest = World::new();
28
29    // Populate the forest with creatures. Some are enchanted.
30    for i in 0..10 {
31        let creature = forest
32            .entity()
33            .set(Location {
34                x: i as f32,
35                y: i as f32,
36            })
37            .set(Ability {
38                power: i as f32 * 1.5,
39            });
40
41        if i % 2 == 0 {
42            creature.add::<Enchanted>();
43        }
44    }
45
46    // Introduce mystical artifacts into the forest, some of which are also enchanted
47    for i in 0..10 {
48        let artifact = forest
49            .entity()
50            .set(Location { x: -1.0, y: -1.0 }) //to showcase we don't match this
51            .set(ArtifactPower {
52                _magic_level: i as f32 * 2.5,
53            });
54
55        if i % 2 != 0 {
56            // Differentiate enchantment condition to diversify
57            artifact.add::<Enchanted>();
58        }
59    }
60
61    // Query for creatures based on their Location and Ability
62    let query_creatures = forest.query::<(&Location, &Ability)>().set_cached().build();
63
64    // Filter specifically for enchanted things in the world
65    let mut query_enchanted = forest.query::<()>().with::<&Enchanted>().build();
66
67    // Iterate over creatures to find the enchanted ones
68    query_creatures.run_iter(|iter, (loc, ability)| {
69
70        // Filter for enchanted creatures within the current iteration
71        query_enchanted
72            .set_var_table(0, iter.range().unwrap())
73            .each_iter( |it, index ,_| {
74               let pos = &loc[index];
75               let abil_power = ability[index].power;
76               let entity = it.entity(index);
77                println!(
78                    "Creature id: {entity} at location {},{} is enchanted with mystical energy, ability power: {} "
79                    , pos.x, pos.y, abil_power
80
81                );
82            });
83    });
84
85    // Output:
86    //  Creature id: 525 at location 0,0 is enchanted with mystical energy, ability power: 0
87    //  Creature id: 527 at location 2,2 is enchanted with mystical energy, ability power: 3
88    //  Creature id: 529 at location 4,4 is enchanted with mystical energy, ability power: 6
89    //  Creature id: 531 at location 6,6 is enchanted with mystical energy, ability power: 9
90    //  Creature id: 533 at location 8,8 is enchanted with mystical energy, ability power: 12
91}
92
93#[cfg(feature = "flecs_nightly_tests")]
94#[test]
95fn test() {
96    let output_capture = OutputCapture::capture().unwrap();
97    main();
98    output_capture.test("query_chaining_queries".to_string());
99}