Skip to content
Flecs v4.1
entities/fwd_declare_component

  • C

    View on GitHub

    1#include <fwd_declare_component.h>
    2#include <stdio.h>
    3
    4// This example shows how to use utility macro's to forward declare and use a
    5// component. The ECS_COMPONENT automatically declares a variable that holds a
    6// component id, but this variable is scoped to the function only. When trying
    7// to access the component from another function, this typically causes errors
    8// that look like this:
    9// "FLECS_IDComponentNameID" is undefined
    10//
    11// Forward declaring a component will make the component id available from other
    12// functions, which fixes this error.
    13
    14// The component type
    15typedef struct {
    16 double x, y;
    17} Position;
    18
    19// The forward declaration of the component id variable. This variable will have
    20// the name FLECS_IDPositionID, to ensure its name won't conflict with the type.
    21ECS_COMPONENT_DECLARE(Position);
    22
    23// When you want forward declare a component from a header, make sure to use the
    24// extern keyword to prevent multiple definitions of the same variable:
    25//
    26// In the header:
    27// extern ECS_COMPONENT_DECLARE(Position);
    28//
    29// In *one* of the source files:
    30// ECS_COMPONENT_DECLARE(Position);
    31
    32
    33// To forward declare entities created with ECS_ENTITY, ECS_TAG or ECS_PREFAB,
    34// use ECS_DECLARE.
    35ECS_DECLARE(Wizard);
    36
    37// Regular entity handles can also be forward declared
    38ecs_entity_t Platoon_1;
    39
    40ecs_entity_t create_npc(ecs_world_t *world) {
    41 ecs_entity_t result = ecs_new(world);
    42
    43 // Without the forward declaration, this would have thrown a compiler error
    44 ecs_set(world, result, Position, {10, 20});
    45 ecs_add(world, result, Wizard);
    46
    47 // An entity that's not declared using the utility macro's can be added with
    48 // ecs_add_id. This has the same effect, with as only difference that this
    49 // function doesn't look for a variable named FLECS_EPlatoon_1
    50 ecs_add_id(world, result, Platoon_1);
    51
    52 return result;
    53}
    54
    55int main(int argc, char *argv[]) {
    56 ecs_world_t *world = ecs_init_w_args(argc, argv);
    57
    58 // Register the component and assign the forward declared variable.
    59 ECS_COMPONENT_DEFINE(world, Position);
    60
    61 // Same for the tag
    62 ECS_TAG_DEFINE(world, Wizard);
    63
    64 // A forward declared entity can be assigned as any variable
    65 Platoon_1 = ecs_new(world);
    66
    67 // Create new entity with Position
    68 ecs_entity_t e = create_npc(world);
    69 const Position *p = ecs_get(world, e, Position);
    70 printf("{%f, %f}\n", p->x, p->y);
    71
    72 // To access the component id variable directly, use the ecs_id macro:
    73 printf("Id of Position is %u\n",
    74 (uint32_t)ecs_id(Position)); // the cast avoids formatting warnings
    75
    76 return ecs_fini(world);
    77
    78 // Output:
    79 // {10.000000, 20.000000}
    80 // Id of Position is 497
    81}
    void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
    Add a (component) ID to an entity.
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    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_add(world, entity, T)
    Add a component to an entity.
    Definition flecs_c.h:271
    #define ECS_DECLARE(id)
    Forward declare an entity, tag, prefab, or any other entity identifier.
    Definition flecs_c.h:25
    #define ECS_COMPONENT_DEFINE(world, id_)
    Define a forward-declared component.
    Definition flecs_c.h:75
    #define ECS_TAG_DEFINE(world, id_)
    Define a forward-declared tag.
    Definition flecs_c.h:39
    #define ECS_COMPONENT_DECLARE(id)
    Forward declare a component.
    Definition flecs_c.h:65
    #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.