examples/reflection/reflection_world_ser_deser.rs
1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4use json::WorldToJsonDesc;
5
6#[derive(Debug, Component)]
7#[flecs(meta)]
8pub struct Position {
9 pub x: f32,
10 pub y: f32,
11}
12
13#[derive(Debug, Component)]
14#[flecs(meta)]
15pub struct Velocity {
16 pub x: f32,
17 pub y: f32,
18}
19
20#[derive(Component)]
21pub struct Move;
22
23// Register components and systems in a module. This excludes them by default
24// from the serialized data, and makes it easier to import across worlds.
25impl Module for Move {
26 fn module(world: &World) {
27 world
28 .system_named::<(&mut Position, &Velocity)>("Move")
29 .each_entity(|e, (pos, vel)| {
30 pos.x += vel.x;
31 pos.y += vel.y;
32
33 println!(
34 "{} moved to {{x: {}, y: {}}}",
35 e.path().unwrap_or_default(),
36 pos.x,
37 pos.y
38 );
39 });
40 }
41}
42
43/*
44
45int main(int, char *[]) {
46 flecs::world world_a;
47
48 world_a.import<move>();
49
50 world_a.entity("ent_1")
51 .set<Position>({10, 20})
52 .set<Velocity>({1, -1});
53
54 world_a.entity("ent_2")
55 .set<Position>({30, 40})
56 .set<Velocity>({-1, 1});
57
58 // Serialize world to JSON
59 auto json = world_a.to_json();
60 std::cout << json << std::endl << std::endl;
61
62 // Output:
63 // {
64 // "results": [{
65 // "ids": [
66 // ["my_module.Position"],
67 // ["my_module.Velocity"],
68 // ["flecs.core.Identifier", "flecs.core.Name"]
69 // ],
70 // "entities": ["ent_1", "ent_2"],
71 // "values": [
72 // [{
73 // "x": 10,
74 // "y": 20
75 // }, {
76 // "x": 30,
77 // "y": 40
78 // }],
79 // [{
80 // "x": 1,
81 // "y": -1
82 // }, {
83 // "x": -1,
84 // "y": 1
85 // }], 0
86 // ]
87 // }]
88 // }
89
90 // Create second world, import same module
91 flecs::world world_b;
92 world_b.import<move>();
93
94 // Deserialize JSON into second world
95 world_b.from_json(json);
96
97 // Run system once for both worlds
98 world_a.progress();
99 std::cout << std::endl;
100 world_b.progress();
101
102 // Output
103 // ::ent_1 moved to {x: 11, y: 19}
104 // ::ent_2 moved to {x: 29, y: 41}
105 //
106 // ::ent_1 moved to {x: 11, y: 19}
107 // ::ent_2 moved to {x: 29, y: 41}
108}
109
110*/
111
112fn main() {
113 let world_a = World::new();
114
115 world_a.import::<Move>();
116
117 world_a
118 .entity_named("ent_1")
119 .set(Position { x: 10.0, y: 20.0 })
120 .set(Velocity { x: 1.0, y: -1.0 });
121
122 world_a
123 .entity_named("ent_2")
124 .set(Position { x: 30.0, y: 40.0 })
125 .set(Velocity { x: -1.0, y: 1.0 });
126
127 // Serialize world to JSON
128 let json = world_a.to_json_world(None);
129 println!("{json}");
130
131 // Output:
132 // // component info is not included in the Output result, but it's there
133 // {
134 // "name": "ent_1",
135 // "id": 551,
136 // "components": {
137 // "examples.reflection.world_ser_deser.Position": {
138 // "x": 10,
139 // "y": 20
140 // },
141 // "examples.reflection.world_ser_deser.Velocity": {
142 // "x": 1,
143 // "y": -1
144 // },
145 // "(flecs.core.Identifier,flecs.core.Name)": null
146 // }
147 // },
148 // {
149 // "name": "ent_2",
150 // "id": 552,
151 // "components": {
152 // "examples.reflection.world_ser_deser.Position": {
153 // "x": 30,
154 // "y": 40
155 // },
156 // "examples.reflection.world_ser_deser.Velocity": {
157 // "x": -1,
158 // "y": 1
159 // },
160 // "(flecs.core.Identifier,flecs.core.Name)": null
161 // }
162 // }
163
164 // Create second world, import same module
165 let world_b = World::new();
166 world_b.import::<Move>();
167
168 // Deserialize JSON into second world
169 world_b.from_json_world(json.as_str(), None);
170
171 // Run system once for both worlds
172 world_a.progress();
173 println!();
174 world_b.progress();
175
176 // Output
177 // ::ent_1 moved to {x: 11, y: 19}
178 // ::ent_2 moved to {x: 29, y: 41}
179 //
180 // ::ent_1 moved to {x: 11, y: 19}
181 // ::ent_2 moved to {x: 29, y: 41}
182}
183
184#[cfg(feature = "flecs_nightly_tests")]
185#[test]
186fn test() {
187 let output_capture = OutputCapture::capture().unwrap();
188 main();
189 output_capture.test("reflection_world_ser_deser".to_string());
190}