Skip to content
Flecs v4.1
entities/basics

  • C

    View on GitHub

    1#include <basics.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x, y;
    6} Position;
    7
    8int main(int argc, char *argv[]) {
    9 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    10
    11 ECS_COMPONENT(ecs, Position);
    12 ECS_TAG(ecs, Walking);
    13
    14 // Create an entity with name Bob
    15 ecs_entity_t bob = ecs_set_name(ecs, 0, "Bob");
    16
    17 // The set operation finds or creates a component, and sets it.
    18 ecs_set(ecs, bob, Position, {10, 20});
    19 // The add operation adds a component without setting a value. This is
    20 // useful for tags, or when adding a component with its default value.
    21 ecs_add(ecs, bob, Walking);
    22
    23 // Get the value for the Position component
    24 const Position *ptr = ecs_get(ecs, bob, Position);
    25 printf("{%f, %f}\n", ptr->x, ptr->y);
    26
    27 // Overwrite the value of the Position component
    28 ecs_set(ecs, bob, Position, {20, 30});
    29
    30 // Create another named entity
    31 ecs_entity_t alice = ecs_set_name(ecs, 0, "Alice");
    32 ecs_set(ecs, alice, Position, {10, 20});
    33 ecs_add(ecs, alice, Walking);
    34
    35 // Print all the components the entity has. This will output:
    36 // Position, Walking, (Identifier,Name)
    37 char *str = ecs_type_str(ecs, ecs_get_type(ecs, alice));
    38 printf("[%s]\n", str);
    39 ecs_os_free(str);
    40
    41 // Remove tag
    42 ecs_remove(ecs, alice, Walking);
    43
    44 // Iterate all entities with Position
    45 ecs_iter_t it = ecs_each(ecs, Position);
    46 while (ecs_each_next(&it)) {
    47 Position *p = ecs_field(&it, Position, 0);
    48 for (int i = 0; i < it.count; i ++) {
    49 printf("%s: {%f, %f}\n", ecs_get_name(ecs, it.entities[i]),
    50 p[i].x, p[i].y);
    51 }
    52 }
    53
    54 return ecs_fini(ecs);
    55
    56 // Output
    57 // {10.000000, 20.000000}
    58 // [Position, Walking, (Identifier,Name)]
    59 // Alice: {10.000000, 20.000000}
    60 // Bob: {20.000000, 30.000000}
    61}
    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
    bool ecs_each_next(ecs_iter_t *it)
    Progress an iterator created with ecs_each_id().
    const ecs_type_t * ecs_get_type(const ecs_world_t *world, ecs_entity_t entity)
    Get the type of an entity.
    char * ecs_type_str(const ecs_world_t *world, const ecs_type_t *type)
    Convert a type to a string.
    #define ecs_remove(world, entity, T)
    Remove a component from an entity.
    Definition flecs_c.h:279
    #define ecs_add(world, entity, T)
    Add a component to an entity.
    Definition flecs_c.h:271
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ECS_TAG(world, id)
    Declare and define a tag.
    Definition flecs_c.h:59
    #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
    #define ecs_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    #define ecs_each(world, id)
    Iterate entities with a component.
    Definition flecs_c.h:931
    ecs_entity_t ecs_set_name(ecs_world_t *world, ecs_entity_t entity, const char *name)
    Set the name of an entity.
    const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
    Get the name of an entity.
    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.
    Iterator.
    Definition flecs.h:1183
    int32_t count
    Number of entities to iterate.
    Definition flecs.h:1189
    const ecs_entity_t * entities
    Entity identifiers.
    Definition flecs.h:1190

  • C++

    View on GitHub

    1#include <basics.h>
    2#include <iostream>
    3
    4struct Position {
    5 double x, y;
    6};
    7
    8struct Walking { };
    9
    10int main(int, char *[]) {
    11 flecs::world ecs;
    12
    13 // Create an entity with name Bob
    14 flecs::entity bob = ecs.entity("Bob")
    15 // The set operation finds or creates a component, and sets it.
    16 // Components are automatically registered with the world.
    17 .set<Position>({10, 20})
    18 // The add operation adds a component without setting a value. This is
    19 // useful for tags, or when adding a component with its default value.
    20 .add<Walking>();
    21
    22 // Get the value for the Position component
    23 const Position& c = bob.get<Position>();
    24 std::cout << "{" << c.x << ", " << c.y << "}" << "\n";
    25
    26 // Overwrite the value of the Position component
    27 bob.set<Position>({20, 30});
    28
    29 // Create another named entity
    30 flecs::entity alice = ecs.entity("Alice")
    31 .set<Position>({10, 20});
    32
    33 // Add a tag after entity is created
    34 alice.add<Walking>();
    35
    36 // Print all of the components the entity has. This will output:
    37 // Position, Walking, (Identifier,Name)
    38 std::cout << "[" << alice.type().str() << "]" << "\n";
    39
    40 // Remove tag
    41 alice.remove<Walking>();
    42
    43 // Iterate all entities with Position
    44 ecs.each([](flecs::entity e, Position& p) {
    45 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}" << "\n";
    46 });
    47
    48 // Output
    49 // {10, 20}
    50 // [Position, Walking, (Identifier,Name)]
    51 // Alice: {10, 20}
    52 // Bob: {20, 30}
    53}
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.
    Definition impl.hpp:368
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::type type() const
    Get the entity's type.
    Definition impl.hpp:94
    flecs::string_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    flecs::string str() const
    Convert the type to a comma-separated string.
    Definition type.hpp:38
    The world.
    Definition world.hpp:129