Skip to content
Flecs v4.1
script/script_run

  • C

    View on GitHub

    1#include <script_run.h>
    2#include <stdio.h>
    3
    4/* This example shows how to run a script once. To see how to parse a script and
    5 * run it multiple times, see the script_parse_eval example. */
    6
    7const char *script =
    8 "Sun {\n"
    9 " Mercury {}\n"
    10 " Venus {}\n"
    11 " Earth {}\n"
    12 " Mars {}\n"
    13 " Jupiter {}\n"
    14 " Saturn {}\n"
    15 " Neptune {}\n"
    16 " Uranus {}\n"
    17 "}\n"
    18 ;
    19
    20int main(int argc, char *argv[]) {
    21 ecs_world_t *world = ecs_init_w_args(argc, argv);
    22
    23 if (ecs_script_run(world, "My script", script, NULL)) {
    24 printf("script failed to run\n");
    25 }
    26
    27 ecs_entity_t sun = ecs_lookup(world, "Sun");
    28 ecs_iter_t it = ecs_children(world, sun);
    29 while (ecs_children_next(&it)) {
    30 for (int i = 0; i < it.count; i ++) {
    31 printf(" - %s\n", ecs_get_name(world, it.entities[i]));
    32 }
    33 }
    34
    35 return ecs_fini(world);
    36
    37 // Output
    38 // - Mercury
    39 // - Venus
    40 // - Earth
    41 // - Mars
    42 // - Jupiter
    43 // - Saturn
    44 // - Neptune
    45 // - Uranus
    46}
    FLECS_API int ecs_script_run(ecs_world_t *world, const char *name, const char *code, ecs_script_eval_result_t *result)
    Parse script.
    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