Skip to main content

examples/reflection/
reflection_runtime_nested_component.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5fn main() {
6    let world = World::new();
7
8    let point = world
9        .component_untyped_named("Point")
10        .member(f32::id(), "x")
11        .member(f32::id(), "y");
12
13    let line = world
14        .component_untyped_named("Line")
15        .member(point, "start")
16        .member(point, "stop");
17
18    // Create entity, set value of line using reflection API
19    let e = unsafe { world.entity().add_id_unchecked(line) };
20
21    let ptr = e.get_untyped_mut(line);
22
23    let mut cur = unsafe { world.cursor_id(line, ptr) };
24
25    #[rustfmt::skip]
26    fn cursor(cur: &mut Cursor) {
27        cur.push();            // {
28        cur.push();            //   {
29        cur.set_float(10.0);   //     10
30        cur.next();            //     ,
31        cur.set_float(20.0);   //     20
32        cur.pop();             //   }
33        cur.next();            //   ,
34        cur.push();            //   {
35        cur.set_float(30.0);   //     30
36        cur.next();            //     ,
37        cur.set_float(40.0);   //     40
38        cur.pop();             //   }
39        cur.pop();             // }
40    }
41
42    // we use a function to format skip the comments for better understanding.
43    // in normal cases, you can just write the code directly.
44    cursor(&mut cur);
45
46    // Convert component to string
47    println!("{:?}", unsafe { world.to_expr_id(line, ptr) });
48
49    // Output
50    // {start: {x: 10.00, y: 20.00}, stop: {x: 30.00, y: 40.00}}
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_runtime_nested_component".to_string());
59}