Skip to content
Flecs v4.1
systems/startup_system

  • C

    View on GitHub

    1#include <startup_system.h>
    2#include <stdio.h>
    3
    4// Startup systems are systems registered with the EcsOnStart phase, and are
    5// only ran during the first frame. Just like with regular phases, custom phases
    6// can depend on the EcsOnStart phase (see custom_phases example). Phases that
    7// depend on EcsOnStart are also only ran during the first frame.
    8//
    9// Other than that, startup systems behave just like regular systems (they can
    10// match components, can introduce merge points), with as only exception that
    11// they are guaranteed to always run on the main thread.
    12
    13void Startup(ecs_iter_t *it) {
    14 printf("%s\n", ecs_get_name(it->stage, it->system));
    15}
    16
    17void Update(ecs_iter_t *it) {
    18 printf("%s\n", ecs_get_name(it->stage, it->system));
    19}
    20
    21int main(int argc, char *argv[]) {
    22 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    23
    24 // Startup system
    25 ECS_SYSTEM(ecs, Startup, EcsOnStart, 0);
    26
    27 // Regular system
    28 ECS_SYSTEM(ecs, Update, EcsOnUpdate, 0);
    29
    30 // First frame. This runs both the Startup and Update systems
    31 ecs_progress(ecs, 0.0f);
    32
    33 // Second frame. This runs only the Update system
    34 ecs_progress(ecs, 0.0f);
    35
    36 return ecs_fini(ecs);
    37
    38 // Output
    39 // Startup
    40 // Update
    41 // Update
    42}
    const ecs_entity_t EcsOnStart
    OnStart 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, id, phase,...)
    Declare and define a system.
    Definition system.h:263
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    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 <startup_system.h>
    2#include <iostream>
    3
    4// Startup systems are systems registered with the EcsOnStart phase, and are
    5// only ran during the first frame. Just like with regular phases, custom phases
    6// can depend on the EcsOnStart phase (see custom_phases example). Phases that
    7// depend on EcsOnStart are also only ran during the first frame.
    8//
    9// Other than that, startup systems behave just like regular systems (they can
    10// match components, can introduce merge points), with as only exception that
    11// they are guaranteed to always run on the main thread.
    12
    13int main(int, char *[]) {
    14 flecs::world ecs;
    15
    16 // Startup system
    17 ecs.system("Startup")
    18 .kind(flecs::OnStart)
    19 .run([](flecs::iter& it) {
    20 std::cout << it.system().name() << "\n";
    21 });
    22
    23 // Regular system
    24 ecs.system("Update")
    25 .run([](flecs::iter& it) {
    26 std::cout << it.system().name() << "\n";
    27 });
    28
    29 // First frame. This runs both the Startup and Update systems
    30 ecs.progress();
    31
    32 // Second frame. This runs only the Update system
    33 ecs.progress();
    34
    35 // Output
    36 // Startup
    37 // Update
    38 // Update
    39}
    flecs::system system(flecs::entity e) const
    Upcast an entity to a system.
    Definition impl.hpp:181
    bool progress(ecs_ftime_t delta_time=0.0) const
    Progress the world one tick.
    Definition impl.hpp:47
    flecs::string_view name() const
    Return the entity name.
    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
    system & run(Func &&func)
    Replace the system's run callback.
    Definition impl.hpp:108
    The world.
    Definition world.hpp:129