Skip to content
Flecs v4.1
script/script_managed

  • C

    View on GitHub

    1#include <script_managed.h>
    2#include <stdio.h>
    3
    4/* This example shows how to create a managed script. A managed script remains
    5 * in memory after it is ran, and makes it possible to inspect and modify the
    6 * script. Managed scripts can be inspected and modified from the explorer.
    7 *
    8 * Updating a script will delete any entities or components that are no longer
    9 * described by the script.
    10 */
    11
    12void print_planets(ecs_world_t *world) {
    13 ecs_entity_t sun = ecs_lookup(world, "Sun");
    14 ecs_iter_t it = ecs_children(world, sun);
    15 while (ecs_children_next(&it)) {
    16 for (int i = 0; i < it.count; i ++) {
    17 printf(" - %s\n", ecs_get_name(world, it.entities[i]));
    18 }
    19 }
    20}
    21
    22int main(int argc, char *argv[]) {
    23 ecs_world_t *world = ecs_init_w_args(argc, argv);
    24
    25 ecs_entity_t script = ecs_script(world, {
    26 .code =
    27 "Sun {\n"
    28 " Mercury {}\n"
    29 " Venus {}\n"
    30 " Earth {}\n"
    31 " Mars {}\n"
    32 " Jupiter {}\n"
    33 " Saturn {}\n"
    34 " Neptune {}\n"
    35 " Uranus {}\n"
    36 " Pluto {}\n"
    37 "}\n"
    38 });
    39
    40 if (!script) {
    41 printf("script failed to run\n");
    42 }
    43
    44 printf("Before updating script:\n");
    45 print_planets(world);
    46
    47 ecs_script_update(world, script, 0,
    48 "Sun {\n"
    49 " Mercury {}\n"
    50 " Venus {}\n"
    51 " Earth {}\n"
    52 " Mars {}\n"
    53 " Jupiter {}\n"
    54 " Saturn {}\n"
    55 " Neptune {}\n"
    56 " Uranus {}\n"
    57 // Pluto {}\n" not a planet :(
    58 "}\n"
    59 );
    60
    61 printf("\nAfter updating script:\n");
    62 print_planets(world);
    63
    64 return ecs_fini(world);
    65
    66 // Output
    67 // Before updating script:
    68 // - Mercury
    69 // - Venus
    70 // - Earth
    71 // - Mars
    72 // - Jupiter
    73 // - Saturn
    74 // - Neptune
    75 // - Uranus
    76 // - Pluto
    77 //
    78 // After updating script:
    79 // - Mercury
    80 // - Venus
    81 // - Earth
    82 // - Mars
    83 // - Jupiter
    84 // - Saturn
    85 // - Neptune
    86 // - Uranus
    87}
    FLECS_API int ecs_script_update(ecs_world_t *world, ecs_entity_t script, ecs_entity_t instance, const char *code)
    Update script with new code.
    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
    script_builder script(const char *name=nullptr) const
    Build a script.
    Definition mixin.inl:31
    bool ecs_children_next(ecs_iter_t *it)
    Progress an iterator created with ecs_children().
    ecs_iter_t ecs_children(const ecs_world_t *world, ecs_entity_t parent)
    Iterate children of a parent.
    ecs_entity_t ecs_lookup(const ecs_world_t *world, const char *path)
    Look up an entity by its path.
    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