examples/queries/
query_basics.rs1use 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
17fn main() {
18 let world = World::new();
19
20 let query = world.new_query::<(&mut Position, &Velocity)>();
23
24 world
26 .entity_named("e1")
27 .set(Position { x: 10.0, y: 20.0 })
28 .set(Velocity { x: 1.0, y: 2.0 });
29
30 world
31 .entity_named("e2")
32 .set(Position { x: 10.0, y: 20.0 })
33 .set(Velocity { x: 3.0, y: 4.0 });
34
35 world.entity_named("e3").set(Position { x: 10.0, y: 20.0 });
37
38 query.each_entity(|e, (pos, vel)| {
43 pos.x += vel.x;
44 pos.y += vel.y;
45 println!("{}: [{:?}]", e.name(), pos);
46 });
47
48 query.each(|(pos, vel)| {
50 pos.x += vel.x;
51 pos.y += vel.y;
52 println!("[{pos:?}]");
53 });
54
55 query.run(|mut it| {
58 while it.next() {
59 let mut p = it.field_mut::<Position>(0);
60 let v = it.field::<Velocity>(1);
61
62 for i in it.iter() {
63 p[i].x += v[i].x;
64 p[i].y += v[i].y;
65 println!("[{:?}]", p[i]);
66 }
67 }
68 });
69
70 }
78
79#[cfg(feature = "flecs_nightly_tests")]
80#[test]
81fn test() {
82 let output_capture = OutputCapture::capture().unwrap();
83 main();
84 output_capture.test("query_basics".to_string());
85}