Skip to content
Flecs v4.1
systems/custom_phases_no_builtin

  • C

    View on GitHub

    1#include <custom_phases_no_builtin.h>
    2#include <stdio.h>
    3
    4// This application demonstrates how to use custom phases for systems, without
    5// using or branching off of the builtin pipelines.
    6
    7// Dummy system
    8void Sys(ecs_iter_t *it) {
    9 printf("system %s\n", ecs_get_name(it->stage, it->system));
    10}
    11
    12int main(int argc, char *argv[]) {
    13 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    14
    15 // Create three custom phases. Note that the two phases have the Phase tag,
    16 // which is necessary for the builtin pipeline to discover which systems it
    17 // should run.
    18 ecs_entity_t Update = ecs_new_w_id(ecs, EcsPhase);
    19 ecs_entity_t Physics = ecs_new_w_id(ecs, EcsPhase);
    20 ecs_entity_t Collisions = ecs_new_w_id(ecs, EcsPhase);
    21
    22 ecs_add_pair(ecs, Physics, EcsDependsOn, Update);
    23 ecs_add_pair(ecs, Collisions, EcsDependsOn, Physics);
    24
    25 // Create 3 dummy systems.
    26 ecs_system(ecs, {
    27 .entity = ecs_entity(ecs, { .name = "CollisionSystem" }),
    28
    29 .phase = Collisions,
    30 .callback = Sys
    31 });
    32
    33 ecs_system(ecs, {
    34 .entity = ecs_entity(ecs, { .name = "PhysicsSystem" }),
    35
    36 .phase = Physics,
    37 .callback = Sys
    38 });
    39
    40 ecs_system(ecs, {
    41 .entity = ecs_entity(ecs, { .name = "GameSystem" }),
    42
    43 .phase = Update,
    44 .callback = Sys
    45 });
    46
    47 // Run pipeline
    48 ecs_progress(ecs, 0);
    49
    50 return ecs_fini(ecs);
    51
    52 // Output
    53 // system GameSystem
    54 // system PhysicsSystem
    55 // system CollisionSystem
    56}
    const ecs_entity_t EcsDependsOn
    Used to express dependency relationships.
    const ecs_entity_t EcsPhase
    Phase 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_no_builtin.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 three custom phases. Note that the phases have the Phase tag,
    17 // which is necessary for the builtin pipeline to discover which systems it
    18 // should run.
    19 flecs::entity Update = ecs.entity()
    20 .add(flecs::Phase);
    21
    22 flecs::entity Physics = ecs.entity()
    23 .add(flecs::Phase)
    24 .depends_on(Update);
    25
    26 flecs::entity Collisions = ecs.entity()
    27 .add(flecs::Phase)
    28 .depends_on(Physics);
    29
    30 // Create 3 dummy systems.
    31 ecs.system("CollisionSystem")
    32 .kind(Collisions)
    33 .run(Sys);
    34
    35 ecs.system("PhysicsSystem")
    36 .kind(Physics)
    37 .run(Sys);
    38
    39 ecs.system("GameSystem")
    40 .kind(Update)
    41 .run(Sys);
    42
    43 // Run pipeline
    44 ecs.progress();
    45
    46 // Output
    47 // system GameSystem
    48 // system PhysicsSystem
    49 // system CollisionSystem
    50}
    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