Skip to content
Flecs v4.1
reflection/basics

  • C

    View on GitHub

    1#include <basics.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 float x, y;
    6} Position;
    7
    8int main(int argc, char *argv[]) {
    9 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    10
    11 // Register component as usual
    12 ECS_COMPONENT(ecs, Position);
    13
    14 // Add reflection data to component
    15 ecs_struct(ecs, {
    16 .entity = ecs_id(Position), // Make sure to use existing id
    17 .members = {
    18 { .name = "x", .type = ecs_id(ecs_f32_t) }, // builtin float type
    19 { .name = "y", .type = ecs_id(ecs_f32_t) }
    20 }
    21 });
    22
    23 // Create entity with Position as usual
    24 ecs_entity_t ent = ecs_new(ecs);
    25 ecs_set(ecs, ent, Position, {10, 20});
    26
    27 // Convert position component to flecs expression string
    28 const Position *ptr = ecs_get(ecs, ent, Position);
    29 char *str = ecs_ptr_to_expr(ecs, ecs_id(Position), ptr);
    30 printf("%s\n", str); // {x: 10, y: 20}
    31 ecs_os_free(str);
    32
    33 ecs_fini(ecs);
    34}
    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 <basics.h>
    2#include <iostream>
    3
    4struct Position {
    5 float x;
    6 float y;
    7};
    8
    9int main(int, char *[]) {
    10 flecs::world ecs;
    11
    12 // Register component with reflection data
    13 ecs.component<Position>()
    14 .member<float>("x")
    15 .member<float>("y");
    16
    17 // Create entity with Position as usual
    18 flecs::entity e = ecs.entity()
    19 .set<Position>({10, 20});
    20
    21 // Convert position component to flecs expression string
    22 const Position& p = e.get<Position>();
    23 std::cout << ecs.to_expr(&p).c_str() << "\n"; // {x: 10, y: 20}
    24}
    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