Skip to content
Flecs v4.2
script/async_await_c

  • C

    View on GitHub

    1#include <async_await_c.h>
    2#include <stdio.h>
    3
    4/* This example shows how to call an async function from C, without using a
    5 * script. Calling an async function returns a future, which is completed at a
    6 * later point in time. The application can check the status of the future to
    7 * see whether it was resolved with a value, rejected with an error, or
    8 * cancelled.
    9 *
    10 * See the async_await example for how to await async functions from a script.
    11 */
    12
    13// Error id used when a level doesn't exist
    14ECS_TAG_DECLARE(LevelNotFound);
    15
    16// Future of the level that the loader hasn't finished loading yet
    17ecs_script_future_t *pending_load = NULL;
    18int32_t pending_level = 0;
    19
    20void load_level(
    21 const ecs_function_ctx_t *ctx,
    22 int32_t argc,
    23 const ecs_value_t *argv,
    24 ecs_script_future_t *future)
    25{
    26 (void)ctx;
    27 (void)argc;
    28
    29 pending_level = *(int32_t*)argv[0].ptr;
    30 printf("load_level(%d): started\n", pending_level);
    31
    32 // Don't complete the future yet, but store it so the loader can complete
    33 // it later.
    34 pending_load = future;
    35}
    36
    37// Invoked when a pending future is cancelled. This can be used to abort work
    38// that is still in progress.
    39void load_level_cancel(
    40 const ecs_function_ctx_t *ctx,
    41 ecs_script_future_t *future)
    42{
    43 (void)ctx;
    44
    45 printf("load_level(%d): cancelled\n", pending_level);
    46 ecs_script_future_release(future);
    47 pending_load = NULL;
    48}
    49
    50// System that completes the pending load
    51void LoaderProgress(
    52 ecs_iter_t *it)
    53{
    54 (void)it;
    55
    56 if (!pending_load) {
    57 return;
    58 }
    59
    60 if (pending_level <= 3) {
    61 int32_t entity_count = pending_level * 100;
    62 ecs_value_t value = { .type = ecs_id(ecs_i32_t), .ptr = &entity_count };
    63 ecs_script_future_resolve(pending_load, &value);
    64 } else {
    65 // Reject with an error id, so callers can tell what went wrong
    66 ecs_script_future_reject_id(
    67 pending_load, LevelNotFound, "level does not exist");
    68 }
    69
    70 // Release the reference that the load_level callback received
    71 ecs_script_future_release(pending_load);
    72 pending_load = NULL;
    73}
    74
    75int main(int argc, char *argv[]) {
    76 ecs_world_t *world = ecs_init_w_args(argc, argv);
    77
    78 ECS_TAG_DEFINE(world, LevelNotFound);
    79
    80 ECS_SYSTEM(world, LoaderProgress, EcsOnUpdate, 0);
    81
    82 ecs_entity_t load_level_fn = ecs_async_function(world, {
    83 .name = "load_level",
    84 .return_type = ecs_id(ecs_i32_t),
    85 .params = {
    86 { .name = "level", .type = ecs_id(ecs_i32_t) }
    87 },
    88 .callback = load_level,
    89 .cancel = load_level_cancel
    90 });
    91
    92 int32_t level = 1;
    93 ecs_value_t arg = { .type = ecs_id(ecs_i32_t), .ptr = &level };
    94
    95 // Load level 1. The returned future is owned by the application, and must
    96 // be released when it's no longer needed.
    97 ecs_script_future_t *future = ecs_async_function_call(
    98 world, load_level_fn, 1, &arg);
    99
    100 while (ecs_script_future_status(future) == EcsScriptFuturePending) {
    101 ecs_progress(world, 0);
    102 }
    103
    104 const ecs_value_t *value = ecs_script_future_get_value(future);
    105 printf("level 1: loaded %d entities\n", *(int32_t*)value->ptr);
    106 ecs_script_future_release(future);
    107
    108 // Load level 42, which doesn't exist
    109 level = 42;
    110 future = ecs_async_function_call(world, load_level_fn, 1, &arg);
    111
    112 while (ecs_script_future_status(future) == EcsScriptFuturePending) {
    113 ecs_progress(world, 0);
    114 }
    115
    116 char *error_id = ecs_get_path(world,
    117 ecs_script_future_get_error_id(future));
    118 printf("level 42: failed with %s (%s)\n", error_id,
    119 ecs_script_future_get_error(future));
    120 ecs_os_free(error_id);
    121 ecs_script_future_release(future);
    122
    123 // Load level 2, but cancel it before it has finished loading
    124 level = 2;
    125 future = ecs_async_function_call(world, load_level_fn, 1, &arg);
    126 ecs_script_future_cancel(future);
    127
    128 if (ecs_script_future_status(future) == EcsScriptFutureCancelled) {
    129 printf("level 2: cancelled\n");
    130 }
    131
    132 ecs_script_future_release(future);
    133
    134 return ecs_fini(world);
    135
    136 // Output
    137 // load_level(1): started
    138 // level 1: loaded 100 entities
    139 // load_level(42): started
    140 // level 42: failed with LevelNotFound (level does not exist)
    141 // load_level(2): started
    142 // load_level(2): cancelled
    143 // level 2: cancelled
    144}
    const ecs_entity_t EcsOnUpdate
    OnUpdate pipeline phase.
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    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.
    #define ECS_SYSTEM(world, id, phase,...)
    Declare and define a system.
    Definition system.h:266
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:396
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:440
    #define ECS_TAG_DECLARE
    Forward declare a tag.
    Definition flecs_c.h:29
    #define ECS_TAG_DEFINE(world, id_)
    Define a forward-declared tag.
    Definition flecs_c.h:39
    #define ecs_get_path(world, child)
    Get path from root.
    Definition flecs_c.h:569
    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
    Value of a dynamic type.
    Definition flecs.h:1069
    void * ptr
    Pointer to value.
    Definition flecs.h:1071