Skip to content
Flecs v4.1
reflection/nested_struct

  • C

    View on GitHub

    1#include <nested_struct.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 float x, y;
    6} Point;
    7
    8typedef struct {
    9 Point start, stop;
    10} Line;
    11
    12int main(int argc, char *argv[]) {
    13 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    14
    15 ECS_COMPONENT(ecs, Point);
    16 ECS_COMPONENT(ecs, Line);
    17
    18 ecs_struct(ecs, {
    19 .entity = ecs_id(Point),
    20 .members = {
    21 { .name = "x", .type = ecs_id(ecs_f32_t) },
    22 { .name = "y", .type = ecs_id(ecs_f32_t) }
    23 }
    24 });
    25
    26 ecs_struct(ecs, {
    27 .entity = ecs_id(Line),
    28 .members = {
    29 { .name = "start", .type = ecs_id(Point) },
    30 { .name = "stop", .type = ecs_id(Point) }
    31 }
    32 });
    33
    34 ecs_entity_t ent = ecs_new(ecs);
    35 ecs_set(ecs, ent, Line, {{10, 20}, {30, 40}});
    36
    37 const Line *ptr = ecs_get(ecs, ent, Line);
    38 char *str = ecs_ptr_to_expr(ecs, ecs_id(Line), ptr);
    39 printf("%s\n", str); // {start: {x: 10, y: 20}, stop: {x: 30, y: 40}}
    40 ecs_os_free(str);
    41
    42 return ecs_fini(ecs);
    43}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    float ecs_f32_t
    Built-in f32 type.
    Definition meta.h:108
    #define ecs_struct(world,...)
    Create a struct type.
    Definition meta.h:1777
    FLECS_API char * ecs_ptr_to_expr(const ecs_world_t *world, ecs_entity_t type, const void *data)
    Serialize value into expression string.
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:395
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    ecs_entity_t ecs_new(ecs_world_t *world)
    Create new entity ID.
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    #define ecs_get(world, entity, T)
    Get a component.
    Definition flecs_c.h:339
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.

  • C++

    View on GitHub

    1#include <nested_struct.h>
    2#include <iostream>
    3
    4struct Point {
    5 float x;
    6 float y;
    7};
    8
    9struct Line {
    10 Point start;
    11 Point stop;
    12};
    13
    14int main(int, char *[]) {
    15 flecs::world ecs;
    16
    17 // Register components with reflection data
    18 ecs.component<Point>()
    19 .member<float>("x")
    20 .member<float>("y");
    21
    22 ecs.component<Line>()
    23 .member<Point>("start")
    24 .member<Point>("stop");
    25
    26 // Create entity with Line as usual
    27 flecs::entity e = ecs.entity()
    28 .set<Line>({{10, 20}, {30, 40}});
    29
    30 // Convert Line component to flecs expression string
    31 const Line& l = e.get<Line>();
    32 std::cout << ecs.to_expr(&l).c_str() << std::endl;
    33 // {start: {x: 10.00, y: 20.00}, stop: {x: 30.00, y: 40.00}}
    34}
    flecs::string to_expr(flecs::entity_t tid, const void *value)
    Convert a value to a string.
    Definition world.hpp:81
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    Entity.
    Definition entity.hpp:30
    const char * c_str() const
    Return the C string.
    Definition string.hpp:109
    The world.
    Definition world.hpp:129