examples/queries/
query_run.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
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 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 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 println!("Table: {:?}", it.archetype());
54 println!(" - number of entities: {}", it.count());
55 println!();
56
57 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 }
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}