Skip to main content

examples/reflection/
reflection_query_to_json.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5#[derive(Component)]
6#[flecs(meta)]
7pub struct Position {
8    pub x: f32,
9    pub y: f32,
10}
11
12#[derive(Component)]
13#[flecs(meta)]
14pub struct Velocity {
15    pub x: f32,
16    pub y: f32,
17}
18
19#[derive(Component)]
20#[flecs(meta)]
21pub struct Mass {
22    pub value: f32,
23}
24
25fn main() {
26    let world = World::new();
27
28    world
29        .entity_named("a")
30        .set(Position { x: 10.0, y: 20.0 })
31        .set(Velocity { x: 1.0, y: 2.0 });
32
33    world
34        .entity_named("b")
35        .set(Position { x: 20.0, y: 30.0 })
36        .set(Velocity { x: 2.0, y: 3.0 });
37
38    world
39        .entity_named("c")
40        .set(Position { x: 30.0, y: 40.0 })
41        .set(Velocity { x: 3.0, y: 4.0 })
42        .set(Mass { value: 10.0 });
43
44    world
45        .entity_named("d")
46        .set(Position { x: 30.0, y: 40.0 })
47        .set(Velocity { x: 4.0, y: 5.0 })
48        .set(Mass { value: 20.0 });
49
50    // Query for components
51    let q = world.new_query::<(&mut Position, &Velocity)>();
52
53    println!("{}", q.iterable().to_json(None).unwrap());
54
55    // Output:
56    //  {
57    //      "results": [
58    //        {
59    //          "name": "a",
60    //          "fields": {
61    //            "values": [
62    //              {
63    //                "x": 10,
64    //                "y": 20
65    //              },
66    //              {
67    //                "x": 1,
68    //                "y": 2
69    //              }
70    //            ]
71    //          }
72    //        },
73    //        {
74    //          "name": "b",
75    //          "fields": {
76    //            "values": [
77    //              {
78    //                "x": 20,
79    //                "y": 30
80    //              },
81    //              {
82    //                "x": 2,
83    //                "y": 3
84    //              }
85    //            ]
86    //          }
87    //        },
88    //        {
89    //          "name": "c",
90    //          "fields": {
91    //            "values": [
92    //              {
93    //                "x": 30,
94    //                "y": 40
95    //              },
96    //              {
97    //                "x": 3,
98    //                "y": 4
99    //              }
100    //            ]
101    //          }
102    //        },
103    //        {
104    //          "name": "d",
105    //          "fields": {
106    //            "values": [
107    //              {
108    //                "x": 30,
109    //                "y": 40
110    //              },
111    //              {
112    //                "x": 4,
113    //                "y": 5
114    //              }
115    //            ]
116    //          }
117    //        }
118    //      ]
119    //    }
120}
121
122#[cfg(feature = "flecs_nightly_tests")]
123#[test]
124fn test() {
125    let output_capture = OutputCapture::capture().unwrap();
126    main();
127    output_capture.test("reflection_query_to_json".to_string());
128}