Skip to content
Flecs v4.1
reflection/auto_define_struct

  • C

    View on GitHub

    1#include <auto_define_struct.h>
    2#include <stdio.h>
    3
    4// Captures reflection data for Position
    5ECS_STRUCT(Position, {
    6 float x;
    7 float y;
    8});
    9
    10int main(int argc, char *argv[]) {
    11 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    12
    13 // This registers the component with reflection data
    14 ECS_META_COMPONENT(ecs, Position);
    15
    16 // Serialize as usual
    17 Position value = {10, 20};
    18 char *json = ecs_ptr_to_json(ecs, ecs_id(Position), &value);
    19 printf("%s\n", json); // {"x": 10.0, "y": 20.0}
    20 ecs_os_free(json);
    21
    22 return ecs_fini(ecs);
    23}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    FLECS_API char * ecs_ptr_to_json(const ecs_world_t *world, ecs_entity_t type, const void *data)
    Serialize value into JSON string.
    #define ECS_META_COMPONENT(world, name)
    Declare component with descriptor.
    Definition meta_c.h:40
    #define ECS_STRUCT(name,...)
    ECS_STRUCT(name, body).
    Definition meta_c.h:46
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    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 <auto_define_struct.h>
    2#include <iostream>
    3
    4// Capture reflection data for Position
    5ECS_STRUCT(Position, {
    6 float x;
    7 float y;
    8});
    9
    10int main(int, char *[]) {
    11 flecs::world ecs;
    12
    13 // Serialize as usual. Component registration automatically detects the
    14 // reflection data.
    15 Position value = {10, 20};
    16 std::cout << ecs.to_json(&value) << "\n"; // {"x":10, "y":20}
    17}
    flecs::string to_json(flecs::entity_t tid, const void *value) const
    Serialize an untyped value to JSON.
    Definition world.hpp:12
    The world.
    Definition world.hpp:129