Skip to content
Flecs v4.1
reflection/basics_enum

  • C

    View on GitHub

    1#include <basics_enum.h>
    2#include <stdio.h>
    3
    4typedef enum {
    5 Red,
    6 Green,
    7 Blue
    8} Color;
    9
    10typedef struct {
    11 Color color;
    12} TypeWithEnum;
    13
    14int main(int argc, char *argv[]) {
    15 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    16
    17 // Register component as usual
    18 ECS_COMPONENT(ecs, Color);
    19 ECS_COMPONENT(ecs, TypeWithEnum);
    20
    21 // Add reflection data to components
    22 ecs_enum(ecs, {
    23 .entity = ecs_id(Color), // Make sure to use existing id
    24 .constants = {
    25 { .name = "Red", .value = Red },
    26 { .name = "Green", .value = Green },
    27 { .name = "Blue", .value = Blue }
    28 }
    29 });
    30
    31 ecs_struct(ecs, {
    32 .entity = ecs_id(TypeWithEnum), // Make sure to use existing id
    33 .members = {
    34 { .name = "color", .type = ecs_id(Color) }
    35 }
    36 });
    37
    38 // Create entity with TypeWithEnum as usual
    39 ecs_entity_t ent = ecs_new(ecs);
    40 ecs_set(ecs, ent, TypeWithEnum, {Green});
    41
    42 // Convert TypeWithEnum component to flecs expression string
    43 const TypeWithEnum *ptr = ecs_get(ecs, ent, TypeWithEnum);
    44 char *str = ecs_ptr_to_expr(ecs, ecs_id(TypeWithEnum), ptr);
    45 printf("%s\n", str); // {color: Green}
    46 ecs_os_free(str);
    47
    48 ecs_fini(ecs);
    49}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    #define ecs_enum(world,...)
    Create an enum type.
    Definition meta.h:1753
    #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_enum.h>
    2#include <iostream>
    3
    4enum Color {
    5 Red,
    6 Green,
    7 Blue
    8};
    9
    10struct TypeWithEnum {
    11 Color color;
    12};
    13
    14int main(int, char *[]) {
    15 flecs::world ecs;
    16
    17 // Register components with reflection data
    18 ecs.component<Color>()
    19 .constant("Red", Red)
    20 .constant("Green", Green)
    21 .constant("Blue", Blue);
    22
    23 ecs.component<TypeWithEnum>()
    24 .member<Color>("color");
    25
    26 // Create entity with TypeWithEnum as usual
    27 flecs::entity e = ecs.entity()
    28 .set<TypeWithEnum>({Green});
    29
    30 // Convert TypeWithEnum component to flecs expression string
    31 const TypeWithEnum& c = e.get<TypeWithEnum>();
    32 std::cout << ecs.to_expr(&c).c_str() << "\n"; // {color: Green}
    33}
    component< T > & constant(const char *name, T value)
    Add a constant.
    Definition component.inl:38
    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