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
36
37typedef struct ecs_script_template_t ecs_script_template_t;
38
40typedef struct ecs_script_var_t {
41 const char *name;
42 ecs_value_t value;
43 const ecs_type_info_t *type_info;
45
47typedef 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;
57
59typedef struct ecs_script_t {
60 ecs_world_t *world;
61 const char *name;
62 const char *code;
64
68typedef struct EcsScript {
69 ecs_script_t *script;
70 ecs_script_template_t *template_; /* Only set for template scripts */
72
73
74/* Parsing & running scripts */
75
85FLECS_API
87 ecs_world_t *world,
88 const char *name,
89 const char *code);
90
97FLECS_API
100
110FLECS_API
113
129FLECS_API
131 ecs_world_t *world,
132 const char *name,
133 const char *code);
134
144FLECS_API
146 ecs_world_t *world,
147 const char *filename);
148
157FLECS_API
160 ecs_strbuf_t *buf);
161
169FLECS_API
172
173
174/* Managed scripts (script associated with entity that outlives the function) */
175
177typedef 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 */
182
193FLECS_API
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
210 ecs_world_t *world,
212 ecs_entity_t instance,
213 const char *code);
214
221FLECS_API
223 ecs_world_t *world,
225 ecs_entity_t instance);
226
227
228/* Script variables */
229
246FLECS_API
248 ecs_world_t *world);
249
256FLECS_API
258 ecs_script_vars_t *vars);
259
271FLECS_API
273 ecs_script_vars_t *parent);
274
283FLECS_API
285 ecs_script_vars_t *vars);
286
299FLECS_API
301 ecs_script_vars_t *vars,
302 const char *name);
303
319FLECS_API
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
339 const ecs_script_vars_t *vars,
340 const char *name);
341
374FLECS_API
376 const ecs_iter_t *it,
377 ecs_script_vars_t *vars,
378 int offset);
379
380
381/* Standalone expression evaluation */
382
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;
394
409FLECS_API
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
430 ecs_world_t *world,
431 const char *str,
432 const ecs_script_vars_t *vars);
433
434
435/* Value serialization */
436
446FLECS_API
448 const ecs_world_t *world,
449 ecs_entity_t type,
450 const void *data);
451
461FLECS_API
463 const ecs_world_t *world,
464 ecs_entity_t type,
465 const void *data,
466 ecs_strbuf_t *buf);
467
479FLECS_API
481 const ecs_world_t *world,
482 ecs_entity_t type,
483 const void *data);
484
494FLECS_API
496 const ecs_world_t *world,
497 ecs_entity_t type,
498 const void *data,
499 ecs_strbuf_t *buf);
500
509FLECS_API
511 ecs_world_t *world);
512
513#ifdef __cplusplus
514}
515#endif
516
517#endif
518
521#endif
FLECS_API int ecs_script_ast_to_buf(ecs_script_t *script, ecs_strbuf_t *buf)
Convert script AST to string.
struct ecs_script_expr_run_desc_t ecs_script_expr_run_desc_t
Used with ecs_script_expr_run().
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.
FLECS_API void ecs_script_vars_fini(ecs_script_vars_t *vars)
Free variable scope.
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.
struct EcsScript EcsScript
Script component.
FLECS_API char * ecs_ptr_to_expr(const ecs_world_t *world, ecs_entity_t type, const void *data)
Serialize value into expression string.
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.
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.
FLECS_API ecs_entity_t ecs_script_init(ecs_world_t *world, const ecs_script_desc_t *desc)
Load managed script.
FLECS_API char * ecs_script_ast_to_str(ecs_script_t *script)
Convert script AST to string.
FLECS_API ecs_script_vars_t * ecs_script_vars_pop(ecs_script_vars_t *vars)
Pop variable scope.
FLECS_API int ecs_script_run(ecs_world_t *world, const char *name, const char *code)
Parse script.
FLECS_API void ecs_script_clear(ecs_world_t *world, ecs_entity_t script, ecs_entity_t instance)
Clear all entities associated with script.
FLECS_API ecs_script_var_t * ecs_script_vars_declare(ecs_script_vars_t *vars, const char *name)
Declare a variable.
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.
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.
FLECS_API void ecs_script_free(ecs_script_t *script)
Free script.
struct ecs_script_t ecs_script_t
Script object.
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.
struct ecs_script_desc_t ecs_script_desc_t
Used with ecs_script_init()
FLECS_API ecs_script_vars_t * ecs_script_vars_init(ecs_world_t *world)
Create new variable scope.
FLECS_API ecs_script_vars_t * ecs_script_vars_push(ecs_script_vars_t *parent)
Push new variable scope.
struct ecs_script_vars_t ecs_script_vars_t
Script variable scope.
FLECS_API void FlecsScriptImport(ecs_world_t *world)
Script module import function.
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.
FLECS_API ecs_script_t * ecs_script_parse(ecs_world_t *world, const char *name, const char *code)
Parse script.
struct ecs_script_var_t ecs_script_var_t
Script variable.
FLECS_API int ecs_script_run_file(ecs_world_t *world, const char *filename)
Parse script file.
FLECS_API ecs_script_var_t * ecs_script_vars_lookup(const ecs_script_vars_t *vars, const char *name)
Lookup a variable.
FLECS_API int ecs_script_eval(ecs_script_t *script)
Evaluate script.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:340
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:384
script_builder script(const char *name=nullptr) const
Build script.
Definition mixin.inl:31
#define ECS_COMPONENT_DECLARE(id)
Forward declare a component.
Definition flecs_c.h:112
Script component.
Definition script.h:68
Iterator.
Definition flecs.h:1053
Used with ecs_script_init()
Definition script.h:177
Used with ecs_script_expr_run().
Definition script.h:384
Script object.
Definition script.h:59
Script variable.
Definition script.h:40
Script variable scope.
Definition script.h:47
Type that contains component information (passed to ctors/dtors/...)
Definition flecs.h:906
Utility to hold a value of a dynamic type.
Definition flecs.h:920