Skip to main content

examples/reflection/
reflection_basics.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5#[derive(Debug, Component)]
6#[flecs(meta)]
7pub struct Position {
8    pub x: f32,
9    pub y: f32,
10}
11
12fn main() {
13    let mut world = World::new();
14
15    /* Alternatively without the meta attribute,
16    you can do it manually like so (without the derive macro)
17        .member(f32::id(),"x", 1 /* count */, core::mem::offset_of!(Position, x))
18        .member(f32::id(),"y", 1, core::mem::offset_of!(Position, y));
19    */
20
21    // Create a new entity
22    let e = world.entity().set(Position { x: 2.0, y: 4.0 });
23
24    // Convert position component to flecs expression string
25    e.get::<&Position>(|p| {
26        let expr: String = world.to_expr(p);
27        println!("Position: {expr}");
28    });
29
30    // Output:
31    //  Position: {x: 2, y: 4}
32}
33
34#[cfg(feature = "flecs_nightly_tests")]
35#[test]
36fn test() {
37    let output_capture = OutputCapture::capture().unwrap();
38    main();
39    output_capture.test("reflection_basics".to_string());
40}