Skip to main content

examples/reflection/
reflection_nested_set_member.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5#[derive(Default, Component)]
6#[flecs(meta)]
7pub struct Point {
8    pub x: f32,
9    pub y: f32,
10}
11
12#[derive(Default, Component)]
13#[flecs(meta)]
14pub struct Line {
15    pub start: Point,
16    pub stop: Point,
17}
18
19fn main() {
20    let world = World::new();
21
22    // Create entity, set value of Line using reflection API
23    let e = world.entity().add(Line::id());
24
25    e.get::<&mut Line>(|line| {
26        let mut cur = world.cursor(line);
27
28        cur.push(); // {
29        cur.member("start"); //   start:
30        cur.push(); //   {
31        cur.member("x"); //     x:
32        cur.set_float(10.0); //     10
33        cur.member("y"); //     y:
34        cur.set_float(20.0); //     20
35        cur.pop(); //   }
36        cur.member("stop"); //   stop:
37        cur.push(); //   {
38        cur.member("x"); //     x:
39        cur.set_float(30.0); //     30
40        cur.member("y"); //     y:
41        cur.set_float(40.0); //     40
42        cur.pop(); //   }
43        cur.pop(); // }
44
45        // Convert component to string
46        println!("{}", world.to_expr(line));
47    });
48
49    // Output:
50    //  {start: {x: 10, y: 20}, stop: {x: 30, y: 40}}
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_nested_set_member".to_string());
59}