Skip to content
Flecs v4.2
script/async_block

  • C

    View on GitHub

    1#include <async_block.h>
    2#include <stdio.h>
    3
    4/* This example shows how to await an async function from an async block. Code
    5 * in an async block runs as a task, which is resumed each frame by ecs_progress
    6 * until the block has completed. This means that unlike the async_await
    7 * example, the application doesn't have to create or resume tasks.
    8 *
    9 * An async block can't create entities, but it can assign mut variables. When a
    10 * mut variable is assigned, the parts of the script that depend on it are
    11 * updated automatically.
    12 */
    13
    14const char *code =
    15 "export mut score: i32 = 0\n"
    16 "\n"
    17 "Alice {\n"
    18 " Score: {score}\n"
    19 "}\n"
    20 "\n"
    21 "async {\n"
    22 " const result = await fetch_score(\"Alice\")\n"
    23 " score = result\n"
    24 "}\n"
    25 ;
    26
    27typedef struct {
    28 int32_t value;
    29} Score;
    30
    31// Future of the request that the server hasn't responded to yet
    32ecs_script_future_t *pending_request = NULL;
    33
    34void fetch_score(
    35 const ecs_function_ctx_t *ctx,
    36 int32_t argc,
    37 const ecs_value_t *argv,
    38 ecs_script_future_t *future)
    39{
    40 (void)ctx;
    41 (void)argc;
    42
    43 printf("fetch_score(\"%s\"): request sent\n", *(char**)argv[0].ptr);
    44
    45 // Don't complete the future yet, but store it so the server can complete
    46 // it later.
    47 pending_request = future;
    48}
    49
    50// System that responds to the pending request
    51void ServerProgress(
    52 ecs_iter_t *it)
    53{
    54 (void)it;
    55
    56 if (!pending_request) {
    57 return;
    58 }
    59
    60 // Resolve the future with the result, after which the async block can
    61 // resume. The callback received a reference to the future, which must be
    62 // released after the future is completed.
    63 printf("server: responding with 120\n");
    64 int32_t score = 120;
    65 ecs_value_t value = { .type = ecs_id(ecs_i32_t), .ptr = &score };
    66 ecs_script_future_resolve(pending_request, &value);
    67 ecs_script_future_release(pending_request);
    68 pending_request = NULL;
    69}
    70
    71int main(int argc, char *argv[]) {
    72 ecs_world_t *world = ecs_init_w_args(argc, argv);
    73
    74 ECS_COMPONENT(world, Score);
    75
    76 ecs_struct(world, {
    77 .entity = ecs_id(Score),
    78 .members = {
    79 { .name = "value", .type = ecs_id(ecs_i32_t) }
    80 }
    81 });
    82
    83 ECS_SYSTEM(world, ServerProgress, EcsOnUpdate, 0);
    84
    85 // Register the async function. Unlike regular functions, the callback
    86 // receives a future instead of a result.
    87 ecs_async_function(world, {
    88 .name = "fetch_score",
    89 .return_type = ecs_id(ecs_i32_t),
    90 .params = {
    91 { .name = "name", .type = ecs_id(ecs_string_t) }
    92 },
    93 .callback = fetch_score
    94 });
    95
    96 // Create a managed script. Managed scripts are reactive, which means that
    97 // Alice's Score is updated when the async block assigns the score variable.
    98 ecs_entity_t script = ecs_script(world, {
    99 .code = code
    100 });
    101
    102 if (!script) {
    103 printf("script failed to run\n");
    104 return -1;
    105 }
    106
    107 ecs_entity_t alice = ecs_lookup(world, "Alice");
    108
    109 // Each frame resumes the async block, and runs the ServerProgress system
    110 for (int i = 0; i < 3; i ++) {
    111 ecs_progress(world, 0);
    112 printf("frame %d: Alice: %d\n", i, ecs_get(world, alice, Score)->value);
    113 }
    114
    115 return ecs_fini(world);
    116
    117 // Output
    118 // fetch_score("Alice"): request sent
    119 // server: responding with 120
    120 // frame 0: Alice: 0
    121 // frame 1: Alice: 120
    122 // frame 2: Alice: 120
    123}
    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.
    #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
    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
    Value of a dynamic type.
    Definition flecs.h:1069