examples/reflection/reflection_basics_deserialize.rs
1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5#[derive(Debug, Default, Component)]
6#[flecs(meta)]
7pub struct Position {
8 pub x: f32,
9 pub y: f32,
10}
11
12fn main() {
13 let world = World::new();
14
15 /* Alternatively when not using meta attribute,
16 you can do it manually like so (without the derive macro)
17 .member(f32,"x", 1 /* count */, core::mem::offset_of!(Position, x))
18 .member(f32,"y", 1, core::mem::offset_of!(Position, y));
19 */
20
21 // Create a new entity, set value of position using reflection API
22 let e = world.entity().add(Position::id());
23
24 e.get::<&mut Position>(|pos| {
25 let mut cur = world.cursor::<Position>(pos);
26 cur.push(); // {
27 cur.set_float(10.0); // 10
28 cur.next(); // ,
29 cur.set_float(20.0); // 20
30 cur.pop(); // }
31
32 println!("{}", world.to_expr(pos));
33 });
34
35 // Use member names before assigning values
36 e.get::<&mut Position>(|pos| {
37 let mut cur = world.cursor::<Position>(pos);
38 cur.push(); // {
39 cur.member("y"); // y:
40 cur.set_float(10.0); // 10
41 cur.member("x"); // x:
42 cur.set_float(20.0); // 20
43 cur.pop(); // }
44
45 println!("{}", world.to_expr(pos));
46 });
47
48 // Output:
49 // {x: 10, y: 20}
50 // {x: 20, y: 10}
51}
52
53#[cfg(feature = "flecs_nightly_tests")]
54#[test]
55fn test() {
56 let output_capture = OutputCapture::capture().unwrap();
57 main();
58 output_capture.test("reflection_deserialize".to_string());
59}