1#include <async_block.h>
2#include <stdio.h>
3
4
5
6
7
8
9
10
11
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
32ecs_script_future_t *pending_request = NULL;
33
34void fetch_score(
36 int32_t argc,
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
46
47 pending_request = future;
48}
49
50
51void ServerProgress(
53{
54 (void)it;
55
56 if (!pending_request) {
57 return;
58 }
59
60
61
62
63 printf("server: responding with 120\n");
64 int32_t score = 120;
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[]) {
73
75
78 .members = {
80 }
81 });
82
84
85
86
87 ecs_async_function(world, {
88 .name = "fetch_score",
90 .params = {
92 },
93 .callback = fetch_score
94 });
95
96
97
99 .code = code
100 });
101
103 printf("script failed to run\n");
104 return -1;
105 }
106
108
109
110 for (int i = 0; i < 3; i ++) {
112 printf(
"frame %d: Alice: %d\n", i,
ecs_get(world, alice, Score)->value);
113 }
114
116
117
118
119
120
121
122
123}
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
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.
ecs_id_t ecs_entity_t
An entity identifier.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
script_builder script(const char *name=nullptr) const
Build a script.
#define ECS_COMPONENT(world, id)
Declare and define a component.
#define ecs_get(world, entity, T)
Get a component.
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.