Flecs: include/flecs/addons/script.h Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.0 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
script.h
Go to the documentation of this file.
1
8#ifdef FLECS_SCRIPT
9
18#ifndef FLECS_META
19#define FLECS_META
20#endif
21
22#ifndef FLECS_DOC
23#define FLECS_DOC
24#endif
25
26
27#ifndef FLECS_SCRIPT_H
28#define FLECS_SCRIPT_H
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34FLECS_API
35extern ECS_COMPONENT_DECLARE(EcsScript);
36
37typedef struct ecs_script_template_t ecs_script_template_t;
38
40 typedef struct ecs_script_var_t {
41 const char *name;
42 ecs_value_t value;
43 const ecs_type_info_t *type_info;
44} ecs_script_var_t;
45
47 typedef struct ecs_script_vars_t {
48 struct ecs_script_vars_t *parent;
49 ecs_hashmap_t var_index;
50 ecs_vec_t vars;
51
52 const ecs_world_t *world;
53 struct ecs_stack_t *stack;
54 ecs_stack_cursor_t *cursor;
55 ecs_allocator_t *allocator;
56} ecs_script_vars_t;
57
59 typedef struct ecs_script_t {
60 ecs_world_t *world;
61 const char *name;
62 const char *code;
63} ecs_script_t;
64
68 typedef struct EcsScript {
69 ecs_script_t *script;
70 ecs_script_template_t *template_; /* Only set for template scripts */
71} EcsScript;
72
73
74/* Parsing & running scripts */
75
85FLECS_API
86 ecs_script_t* ecs_script_parse(
87 ecs_world_t *world,
88 const char *name,
89 const char *code);
90
97FLECS_API
98 int ecs_script_eval(
99 ecs_script_t *script);
100
110FLECS_API
111 void ecs_script_free(
112 ecs_script_t *script);
113
129FLECS_API
130 int ecs_script_run(
131 ecs_world_t *world,
132 const char *name,
133 const char *code);
134
144FLECS_API
145 int ecs_script_run_file(
146 ecs_world_t *world,
147 const char *filename);
148
157FLECS_API
158 int ecs_script_ast_to_buf(
159 ecs_script_t *script,
160 ecs_strbuf_t *buf);
161
169FLECS_API
170 char* ecs_script_ast_to_str(
171 ecs_script_t *script);
172
173
174/* Managed scripts (script associated with entity that outlives the function) */
175
177 typedef struct ecs_script_desc_t {
178 ecs_entity_t entity; /* Set to customize entity handle associated with script */
179 const char *filename; /* Set to load script from file */
180 const char *code; /* Set to parse script from string */
181} ecs_script_desc_t;
182
193FLECS_API
194 ecs_entity_t ecs_script_init(
195 ecs_world_t *world,
196 const ecs_script_desc_t *desc);
197
198#define ecs_script(world, ...)\
199 ecs_script_init(world, &(ecs_script_desc_t) __VA_ARGS__)
200
208FLECS_API
209 int ecs_script_update(
210 ecs_world_t *world,
211 ecs_entity_t script,
212 ecs_entity_t instance,
213 const char *code);
214
221FLECS_API
222 void ecs_script_clear(
223 ecs_world_t *world,
224 ecs_entity_t script,
225 ecs_entity_t instance);
226
227
228/* Script variables */
229
246FLECS_API
247 ecs_script_vars_t* ecs_script_vars_init(
248 ecs_world_t *world);
249
256FLECS_API
257 void ecs_script_vars_fini(
258 ecs_script_vars_t *vars);
259
271FLECS_API
272 ecs_script_vars_t* ecs_script_vars_push(
273 ecs_script_vars_t *parent);
274
283FLECS_API
284 ecs_script_vars_t* ecs_script_vars_pop(
285 ecs_script_vars_t *vars);
286
299FLECS_API
300 ecs_script_var_t* ecs_script_vars_declare(
301 ecs_script_vars_t *vars,
302 const char *name);
303
319FLECS_API
320 ecs_script_var_t* ecs_script_vars_define_id(
321 ecs_script_vars_t *vars,
322 const char *name,
323 ecs_entity_t type);
324
325#define ecs_script_vars_define(vars, name, type)\
326 ecs_script_vars_define_id(vars, name, ecs_id(type))
327
337FLECS_API
338 ecs_script_var_t* ecs_script_vars_lookup(
339 const ecs_script_vars_t *vars,
340 const char *name);
341
374FLECS_API
375 void ecs_script_vars_from_iter(
376 const ecs_iter_t *it,
377 ecs_script_vars_t *vars,
378 int offset);
379
380
381/* Standalone expression evaluation */
382
384 typedef struct ecs_script_expr_run_desc_t {
385 const char *name;
386 const char *expr;
387 ecs_entity_t (*lookup_action)(
388 const ecs_world_t*,
389 const char *value,
390 void *ctx);
391 void *lookup_ctx;
392 ecs_script_vars_t *vars;
393} ecs_script_expr_run_desc_t;
394
409FLECS_API
410 const char* ecs_script_expr_run(
411 ecs_world_t *world,
412 const char *ptr,
413 ecs_value_t *value,
414 const ecs_script_expr_run_desc_t *desc);
415
428FLECS_API
429 char* ecs_script_string_interpolate(
430 ecs_world_t *world,
431 const char *str,
432 const ecs_script_vars_t *vars);
433
434
435/* Value serialization */
436
446FLECS_API
447 char* ecs_ptr_to_expr(
448 const ecs_world_t *world,
449 ecs_entity_t type,
450 const void *data);
451
461FLECS_API
462 int ecs_ptr_to_expr_buf(
463 const ecs_world_t *world,
464 ecs_entity_t type,
465 const void *data,
466 ecs_strbuf_t *buf);
467
479FLECS_API
480 char* ecs_ptr_to_str(
481 const ecs_world_t *world,
482 ecs_entity_t type,
483 const void *data);
484
494FLECS_API
495 int ecs_ptr_to_str_buf(
496 const ecs_world_t *world,
497 ecs_entity_t type,
498 const void *data,
499 ecs_strbuf_t *buf);
500
509FLECS_API
510 void FlecsScriptImport(
511 ecs_world_t *world);
512
513#ifdef __cplusplus
514}
515#endif
516
517#endif
518
521#endif
ecs_script_ast_to_buf
FLECS_API int ecs_script_ast_to_buf(ecs_script_t *script, ecs_strbuf_t *buf)
Convert script AST to string.
ecs_script_expr_run_desc_t
struct ecs_script_expr_run_desc_t ecs_script_expr_run_desc_t
Used with ecs_script_expr_run().
ecs_ptr_to_expr_buf
FLECS_API int ecs_ptr_to_expr_buf(const ecs_world_t *world, ecs_entity_t type, const void *data, ecs_strbuf_t *buf)
Serialize value into expression buffer.
ecs_script_vars_fini
FLECS_API void ecs_script_vars_fini(ecs_script_vars_t *vars)
Free variable scope.
ecs_script_expr_run
FLECS_API const char * ecs_script_expr_run(ecs_world_t *world, const char *ptr, ecs_value_t *value, const ecs_script_expr_run_desc_t *desc)
Parse standalone expression into value.
EcsScript
struct EcsScript EcsScript
Script component.
ecs_ptr_to_expr
FLECS_API char * ecs_ptr_to_expr(const ecs_world_t *world, ecs_entity_t type, const void *data)
Serialize value into expression string.
ecs_ptr_to_str
FLECS_API char * ecs_ptr_to_str(const ecs_world_t *world, ecs_entity_t type, const void *data)
Similar as ecs_ptr_to_expr(), but serializes values to string.
ecs_script_update
FLECS_API int ecs_script_update(ecs_world_t *world, ecs_entity_t script, ecs_entity_t instance, const char *code)
Update script with new code.
ecs_script_init
FLECS_API ecs_entity_t ecs_script_init(ecs_world_t *world, const ecs_script_desc_t *desc)
Load managed script.
ecs_script_ast_to_str
FLECS_API char * ecs_script_ast_to_str(ecs_script_t *script)
Convert script AST to string.
ecs_script_vars_pop
FLECS_API ecs_script_vars_t * ecs_script_vars_pop(ecs_script_vars_t *vars)
Pop variable scope.
ecs_script_run
FLECS_API int ecs_script_run(ecs_world_t *world, const char *name, const char *code)
Parse script.
ecs_script_clear
FLECS_API void ecs_script_clear(ecs_world_t *world, ecs_entity_t script, ecs_entity_t instance)
Clear all entities associated with script.
ecs_script_vars_declare
FLECS_API ecs_script_var_t * ecs_script_vars_declare(ecs_script_vars_t *vars, const char *name)
Declare a variable.
ecs_script_vars_define_id
FLECS_API ecs_script_var_t * ecs_script_vars_define_id(ecs_script_vars_t *vars, const char *name, ecs_entity_t type)
Define a variable.
ecs_script_vars_from_iter
FLECS_API void ecs_script_vars_from_iter(const ecs_iter_t *it, ecs_script_vars_t *vars, int offset)
Convert iterator to vars This operation converts an iterator to a variable array.
ecs_script_free
FLECS_API void ecs_script_free(ecs_script_t *script)
Free script.
ecs_script_t
struct ecs_script_t ecs_script_t
Script object.
ecs_ptr_to_str_buf
FLECS_API int ecs_ptr_to_str_buf(const ecs_world_t *world, ecs_entity_t type, const void *data, ecs_strbuf_t *buf)
Serialize value into string buffer.
ecs_script_desc_t
struct ecs_script_desc_t ecs_script_desc_t
Used with ecs_script_init()
ecs_script_vars_init
FLECS_API ecs_script_vars_t * ecs_script_vars_init(ecs_world_t *world)
Create new variable scope.
ecs_script_vars_push
FLECS_API ecs_script_vars_t * ecs_script_vars_push(ecs_script_vars_t *parent)
Push new variable scope.
ecs_script_vars_t
struct ecs_script_vars_t ecs_script_vars_t
Script variable scope.
FlecsScriptImport
FLECS_API void FlecsScriptImport(ecs_world_t *world)
Script module import function.
ecs_script_string_interpolate
FLECS_API char * ecs_script_string_interpolate(ecs_world_t *world, const char *str, const ecs_script_vars_t *vars)
Evaluate interpolated expressions in string.
ecs_script_parse
FLECS_API ecs_script_t * ecs_script_parse(ecs_world_t *world, const char *name, const char *code)
Parse script.
ecs_script_var_t
struct ecs_script_var_t ecs_script_var_t
Script variable.
ecs_script_run_file
FLECS_API int ecs_script_run_file(ecs_world_t *world, const char *filename)
Parse script file.
ecs_script_vars_lookup
FLECS_API ecs_script_var_t * ecs_script_vars_lookup(const ecs_script_vars_t *vars, const char *name)
Lookup a variable.
ecs_script_eval
FLECS_API int ecs_script_eval(ecs_script_t *script)
Evaluate script.
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:347
ecs_world_t
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:391
script
script_builder script(const char *name=nullptr) const
Build script.
Definition mixin.inl:31
ECS_COMPONENT_DECLARE
#define ECS_COMPONENT_DECLARE(id)
Forward declare a component.
Definition flecs_c.h:112
EcsScript
Script component.
Definition script.h:68
ecs_iter_t
Iterator.
Definition flecs.h:1062
ecs_script_desc_t
Used with ecs_script_init()
Definition script.h:177
ecs_script_expr_run_desc_t
Used with ecs_script_expr_run().
Definition script.h:384
ecs_script_t
Script object.
Definition script.h:59
ecs_script_var_t
Script variable.
Definition script.h:40
ecs_script_vars_t
Script variable scope.
Definition script.h:47
ecs_type_info_t
Type that contains component information (passed to ctors/dtors/...)
Definition flecs.h:916
ecs_value_t
Utility to hold a value of a dynamic type.
Definition flecs.h:929