Skip to content
Flecs v4.1
systems/basics

  • C

    View on GitHub

    1#include <basics.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x, y;
    6} Position, Velocity;
    7
    8void Move(ecs_iter_t *it) {
    9 Position *p = ecs_field(it, Position, 0);
    10 const Velocity *v = ecs_field(it, Velocity, 1);
    11
    12 for (int i = 0; i < it->count; i ++) {
    13 p[i].x += v[i].x;
    14 p[i].y += v[i].y;
    15 printf("%s: {%f, %f}\n", ecs_get_name(it->stage, it->entities[i]),
    16 p[i].x, p[i].y);
    17 }
    18}
    19
    20int main(int argc, char *argv[]) {
    21 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    22
    23 ECS_COMPONENT(ecs, Position);
    24 ECS_COMPONENT(ecs, Velocity);
    25
    26 // Create a system for Position, Velocity.
    27 ecs_entity_t move = ecs_system(ecs, {
    28 // Systems are entities, and by initializing the .entity field we can
    29 // set some additional properties for the system like a name. While this
    30 // is not mandatory, it makes a system easier to find in tools like the
    31 // explorer (https://www.flecs.dev/explorer/).
    32 .entity = ecs_entity(ecs, {
    33 .name = "Move"
    34 }),
    35 .query.terms = {
    36 { .id = ecs_id(Position) },
    37 { .id = ecs_id(Velocity), .inout = EcsIn }
    38 },
    39 .callback = Move
    40 });
    41
    42 // C applications can also use the ECS_SYSTEM convenience macro:
    43 // ECS_SYSTEM(ecs, Move, 0, Position, [in] Velocity);
    44
    45 // Create a few test entities for a Position, Velocity query
    46 ecs_entity_t e1 = ecs_entity(ecs, { .name = "e1" });
    47 ecs_set(ecs, e1, Position, {10, 20});
    48 ecs_set(ecs, e1, Velocity, {1, 2});
    49
    50 ecs_entity_t e2 = ecs_entity(ecs, { .name = "e2" });
    51 ecs_set(ecs, e2, Position, {10, 20});
    52 ecs_set(ecs, e2, Velocity, {3, 4});
    53
    54 // This entity will not match as it does not have Position, Velocity
    55 ecs_entity_t e3 = ecs_entity(ecs, { .name = "e3" });
    56 ecs_set(ecs, e3, Position, {10, 20});
    57
    58 // Run the system
    59 ecs_run(ecs, move, 0.0f, NULL);
    60
    61 return ecs_fini(ecs);
    62}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    #define ecs_system(world,...)
    Shorthand for creating a system with ecs_system_init().
    Definition system.h:285
    FLECS_API ecs_entity_t ecs_run(ecs_world_t *world, ecs_entity_t system, ecs_ftime_t delta_time, void *param)
    Run a specific system manually.
    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
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #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_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
    Get the name of an entity.
    @ EcsIn
    Term is only read.
    Definition flecs.h:731
    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
    conditional_t< sizeof...(Args)==0 &&_::is_sparse_query< Comps... >::value, flecs::sparse_query< Comps... >, flecs::query< Comps... > > query(Args &&... args) const
    Create a query.

  • C++

    View on GitHub

    1#include <systems.h>
    2#include <iostream>
    3
    4struct Position {
    5 double x, y;
    6};
    7
    8struct Velocity {
    9 double x, y;
    10};
    11
    12int main(int, char *[]) {
    13 flecs::world ecs;
    14
    15 // Create a system for Position, Velocity. Systems are like queries (see
    16 // queries) with a function that can be ran or scheduled (see pipeline).
    17 flecs::system s = ecs.system<Position, const Velocity>()
    18 .each([](flecs::entity e, Position& p, const Velocity& v) {
    19 p.x += v.x;
    20 p.y += v.y;
    21 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    22 });
    23
    24 // Create a few test entities for a Position, Velocity query
    25 ecs.entity("e1")
    26 .set<Position>({10, 20})
    27 .set<Velocity>({1, 2});
    28
    29 ecs.entity("e2")
    30 .set<Position>({10, 20})
    31 .set<Velocity>({3, 4});
    32
    33 // This entity will not match as it does not have Position, Velocity
    34 ecs.entity("e3")
    35 .set<Position>({10, 20});
    36
    37 // Run the system
    38 s.run();
    39}
    flecs::system system(flecs::entity e) const
    Upcast an entity to a system.
    Definition impl.hpp:181
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::string_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    System.
    Definition impl.hpp:78
    system & run(Func &&func)
    Replace the system's run callback.
    Definition impl.hpp:108
    The world.
    Definition world.hpp:129
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.