Skip to content
Flecs v4.2
script/async_await

  • C

    View on GitHub

    1#include <async_await.h>
    2#include <stdio.h>
    3
    4/* This example shows how to register an async function in C, and how to await
    5 * it from a script. Async functions don't return a result right away. Instead
    6 * they receive a future, which can be completed at a later point in time. A
    7 * script that awaits a future suspends until the future is completed.
    8 *
    9 * Scripts that use await must run as a task. A task is resumed by the
    10 * application until the script has completed.
    11 */
    12
    13const char *code =
    14 "const score = await fetch_score(\"Alice\")\n"
    15 "\n"
    16 "Alice {\n"
    17 " Score: {score}\n"
    18 "}\n"
    19 ;
    20
    21typedef struct {
    22 int32_t value;
    23} Score;
    24
    25// Future of the request that the server hasn't responded to yet
    26ecs_script_future_t *pending_request = NULL;
    27
    28void fetch_score(
    29 const ecs_function_ctx_t *ctx,
    30 int32_t argc,
    31 const ecs_value_t *argv,
    32 ecs_script_future_t *future)
    33{
    34 (void)ctx;
    35 (void)argc;
    36
    37 printf("fetch_score(\"%s\"): request sent\n", *(char**)argv[0].ptr);
    38
    39 // Don't complete the future yet, but store it so the server can complete
    40 // it later.
    41 pending_request = future;
    42}
    43
    44// System that responds to the pending request
    45void ServerProgress(
    46 ecs_iter_t *it)
    47{
    48 (void)it;
    49
    50 if (!pending_request) {
    51 return;
    52 }
    53
    54 // Resolve the future with the result, after which the script can resume.
    55 // The callback received a reference to the future, which must be released
    56 // after the future is completed.
    57 printf("server: responding with 120\n");
    58 int32_t score = 120;
    59 ecs_value_t value = { .type = ecs_id(ecs_i32_t), .ptr = &score };
    60 ecs_script_future_resolve(pending_request, &value);
    61 ecs_script_future_release(pending_request);
    62 pending_request = NULL;
    63}
    64
    65int main(int argc, char *argv[]) {
    66 ecs_world_t *world = ecs_init_w_args(argc, argv);
    67
    68 ECS_COMPONENT(world, Score);
    69
    70 ecs_struct(world, {
    71 .entity = ecs_id(Score),
    72 .members = {
    73 { .name = "value", .type = ecs_id(ecs_i32_t) }
    74 }
    75 });
    76
    77 ECS_SYSTEM(world, ServerProgress, EcsOnUpdate, 0);
    78
    79 // Register the async function. Unlike regular functions, the callback
    80 // receives a future instead of a result.
    81 ecs_async_function(world, {
    82 .name = "fetch_score",
    83 .return_type = ecs_id(ecs_i32_t),
    84 .params = {
    85 { .name = "name", .type = ecs_id(ecs_string_t) }
    86 },
    87 .callback = fetch_score
    88 });
    89
    90 ecs_script_t *script = ecs_script_parse(world, "scores", code, NULL, NULL);
    91 if (!script) {
    92 printf("script failed to parse\n");
    93 return -1;
    94 }
    95
    96 // Create a task for the script. A task runs a script until it awaits a
    97 // future that hasn't been completed yet.
    98 ecs_script_task_t *task = ecs_script_task_new(script, NULL);
    99
    100 // Resume the task each frame until the script is done
    101 ecs_script_task_status_t status;
    102 while ((status = ecs_script_task_resume(task, NULL)) ==
    103 EcsScriptTaskPending)
    104 {
    105 printf("script is waiting\n");
    106 ecs_progress(world, 0);
    107 }
    108
    109 if (status != EcsScriptTaskDone) {
    110 printf("script failed to run\n");
    111 return -1;
    112 }
    113
    114 ecs_script_task_free(task);
    116
    117 const Score *s = ecs_get(world, ecs_lookup(world, "Alice"), Score);
    118 printf("Alice: %d\n", s->value);
    119
    120 return ecs_fini(world);
    121
    122 // Output
    123 // fetch_score("Alice"): request sent
    124 // script is waiting
    125 // server: responding with 120
    126 // Alice: 120
    127}
    const ecs_entity_t EcsOnUpdate
    OnUpdate pipeline phase.
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    char * ecs_string_t
    Built-in string type.
    Definition meta.h:110
    #define ecs_struct(world,...)
    Create a struct type.
    Definition meta.h:1838
    int32_t ecs_i32_t
    Built-in i32 type.
    Definition meta.h:105
    FLECS_API bool ecs_progress(ecs_world_t *world, ecs_ftime_t delta_time)
    Progress a world.
    FLECS_API void ecs_script_free(ecs_script_t *script)
    Free script.
    FLECS_API ecs_script_t * ecs_script_parse(ecs_world_t *world, const char *name, const char *code, const ecs_script_eval_desc_t *desc, ecs_script_eval_result_t *result)
    Parse script.
    #define ECS_SYSTEM(world, id, phase,...)
    Declare and define a system.
    Definition system.h:266
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:440
    script_builder script(const char *name=nullptr) const
    Build a script.
    Definition mixin.inl:31
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:99
    #define ecs_get(world, entity, T)
    Get a component.
    Definition flecs_c.h:342
    ecs_entity_t ecs_lookup(const ecs_world_t *world, const char *path)
    Look up an entity by its path.
    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.
    Script function context.
    Definition script.h:150
    Iterator.
    Definition flecs.h:1184
    Script object.
    Definition script.h:111
    Value of a dynamic type.
    Definition flecs.h:1069