1#include <async_await_c.h>
2#include <stdio.h>
3
4
5
6
7
8
9
10
11
12
13
15
16
17ecs_script_future_t *pending_load = NULL;
18int32_t pending_level = 0;
19
20void load_level(
22 int32_t argc,
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
33
34 pending_load = future;
35}
36
37
38
39void load_level_cancel(
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
51void LoaderProgress(
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;
63 ecs_script_future_resolve(pending_load, &value);
64 } else {
65
66 ecs_script_future_reject_id(
67 pending_load, LevelNotFound, "level does not exist");
68 }
69
70
71 ecs_script_future_release(pending_load);
72 pending_load = NULL;
73}
74
75int main(int argc, char *argv[]) {
77
79
81
83 .name = "load_level",
85 .params = {
87 },
88 .callback = load_level,
89 .cancel = load_level_cancel
90 });
91
92 int32_t level = 1;
94
95
96
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) {
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
109 level = 42;
110 future = ecs_async_function_call(world, load_level_fn, 1, &arg);
111
112 while (ecs_script_future_status(future) == EcsScriptFuturePending) {
114 }
115
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
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
135
136
137
138
139
140
141
142
143
144}
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.
#define ECS_TAG_DECLARE
Forward declare a tag.
#define ECS_TAG_DEFINE(world, id_)
Define a forward-declared tag.
#define ecs_get_path(world, child)
Get path from root.
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.
void * ptr
Pointer to value.