Skip to content
Flecs v4.1
systems/custom_phases

  • C

    View on GitHub

    1#include <custom_phases.h>
    2#include <stdio.h>
    3
    4// This application demonstrates how to use custom phases for systems. The
    5// default pipeline will automatically run systems for custom phases as long as
    6// they have the EcsPhase tag.
    7
    8// Dummy system
    9void Sys(ecs_iter_t *it) {
    10 printf("system %s\n", ecs_get_name(it->stage, it->system));
    11}
    12
    13int main(int argc, char *argv[]) {
    14 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    15
    16 // Create two custom phases that branch off of EcsOnUpdate. Note that the
    17 // phases have the Phase tag, which is necessary for the builtin pipeline
    18 // to discover which systems it should run.
    19 ecs_entity_t Physics = ecs_new_w_id(ecs, EcsPhase);
    20 ecs_entity_t Collisions = ecs_new_w_id(ecs, EcsPhase);
    22 ecs_add_pair(ecs, Collisions, EcsDependsOn, Physics);
    23
    24 // Create 3 dummy systems.
    25 ecs_system(ecs, {
    26 .entity = ecs_entity(ecs, { .name = "CollisionSystem" }),
    27
    28 .phase = Collisions,
    29 .callback = Sys
    30 });
    31
    32 ecs_system(ecs, {
    33 .entity = ecs_entity(ecs, { .name = "PhysicsSystem" }),
    34
    35 .phase = Physics,
    36 .callback = Sys
    37 });
    38
    39 ecs_system(ecs, {
    40 .entity = ecs_entity(ecs, { .name = "GameSystem" }),
    41
    42 .phase = EcsOnUpdate,
    43 .callback = Sys
    44 });
    45
    46 // Run pipeline
    47 ecs_progress(ecs, 0);
    48
    49 return ecs_fini(ecs);
    50
    51 // Output
    52 // system GameSystem
    53 // system PhysicsSystem
    54 // system CollisionSystem
    55}
    const ecs_entity_t EcsDependsOn
    Used to express dependency relationships.
    const ecs_entity_t EcsPhase
    Phase pipeline phase.
    const ecs_entity_t EcsOnUpdate
    OnUpdate pipeline phase.
    FLECS_API bool ecs_progress(ecs_world_t *world, ecs_ftime_t delta_time)
    Progress a world.
    #define ecs_system(world,...)
    Shorthand for creating a system with ecs_system_init().
    Definition system.h:285
    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_w_id(ecs_world_t *world, ecs_id_t component)
    Create new entity with (component) ID.
    #define ecs_add_pair(world, subject, first, second)
    Add a pair to an entity.
    Definition flecs_c.h:275
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    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
    ecs_entity_t system
    The system (if applicable).
    Definition flecs.h:1208

  • C++

    View on GitHub

    1#include <custom_phases.h>
    2#include <iostream>
    3
    4// This application demonstrates how to use custom phases for systems. The
    5// default pipeline will automatically run systems for custom phases as long as
    6// they have the flecs::Phase tag.
    7
    8// Dummy system
    9void Sys(flecs::iter& it) {
    10 std::cout << "system " << it.system().name() << "\n";
    11}
    12
    13int main(int argc, char *argv[]) {
    14 flecs::world ecs(argc, argv);
    15
    16 // Create two custom phases that branch off of EcsOnUpdate. Note that the
    17 // phases have the Phase tag, which is necessary for the builtin pipeline
    18 // to discover which systems it should run.
    19 flecs::entity Physics = ecs.entity()
    20 .add(flecs::Phase)
    21 .depends_on(flecs::OnUpdate);
    22
    23 flecs::entity Collisions = ecs.entity()
    24 .add(flecs::Phase)
    25 .depends_on(Physics);
    26
    27 // Create 3 dummy systems.
    28 ecs.system("CollisionSystem")
    29 .kind(Collisions)
    30 .run(Sys);
    31
    32 ecs.system("PhysicsSystem")
    33 .kind(Physics)
    34 .run(Sys);
    35
    36 ecs.system("GameSystem")
    37 .kind(flecs::OnUpdate)
    38 .run(Sys);
    39
    40 // Run pipeline
    41 ecs.progress();
    42
    43 // Output
    44 // system GameSystem
    45 // system PhysicsSystem
    46 // system CollisionSystem
    47}
    flecs::string_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    entity()
    Default constructor.
    Definition entity.hpp:32
    Class for iterating over query results.
    Definition iter.hpp:68
    flecs::entity system() const
    Get the system entity associated with the iterator.
    Definition iter.hpp:12
    The world.
    Definition world.hpp:129