Skip to main content

examples/reflection/
reflection_runtime_component.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5fn main() {
6    let world = World::new();
7
8    let position = world
9        .component_untyped_named("Position")
10        .member(f32::id(), "x")
11        .member(f32::id(), "y");
12
13    // Create entity
14    let e = world.entity();
15
16    // unchecked add id due to position being uninitialized and not having a ctor.
17    unsafe {
18        e.add_id_unchecked(position);
19    }
20
21    // set value of position using reflection API
22    let ptr = e.get_untyped_mut(position);
23
24    let mut cur = unsafe { world.cursor_id(position, ptr) };
25    cur.push();
26    cur.set_float(10.0);
27    cur.next();
28    cur.set_float(20.0);
29    cur.pop();
30
31    // Convert component to string
32    println!("{:?}", unsafe { world.to_expr_id(position, ptr) });
33
34    // Output
35    //  {x: 10, y: 20}
36}
37
38#[cfg(feature = "flecs_nightly_tests")]
39#[test]
40fn test() {
41    let output_capture = OutputCapture::capture().unwrap();
42    main();
43    output_capture.test("reflection_runtime_component".to_string());
44}