examples/queries/
query_chaining_queries.rs1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4#[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 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 for i in 0..10 {
48 let artifact = forest
49 .entity()
50 .set(Location { x: -1.0, y: -1.0 }) .set(ArtifactPower {
52 _magic_level: i as f32 * 2.5,
53 });
54
55 if i % 2 != 0 {
56 artifact.add::<Enchanted>();
58 }
59 }
60
61 let query_creatures = forest.query::<(&Location, &Ability)>().set_cached().build();
63
64 let mut query_enchanted = forest.query::<()>().with::<&Enchanted>().build();
66
67 query_creatures.run_iter(|iter, (loc, ability)| {
69
70 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 }
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}