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
34#define FLECS_SCRIPT_FUNCTION_ARGS_MAX (16)
35
36FLECS_API
38
39FLECS_API
40extern ECS_DECLARE(EcsScriptTemplate);
41
42FLECS_API
44
45FLECS_API
47
48FLECS_API
50
51/* Script template. */
52typedef struct ecs_script_template_t ecs_script_template_t;
53
55typedef struct ecs_script_var_t {
56 const char *name;
57 ecs_value_t value;
58 const ecs_type_info_t *type_info;
59 bool is_const;
61
63typedef struct ecs_script_vars_t {
64 struct ecs_script_vars_t *parent;
65 ecs_hashmap_t var_index;
66 ecs_vec_t vars;
67
68 const ecs_world_t *world;
69 struct ecs_stack_t *stack;
70 ecs_stack_cursor_t *cursor;
71 ecs_allocator_t *allocator;
73
75typedef struct ecs_script_t {
76 ecs_world_t *world;
77 const char *name;
78 const char *code;
80
81/* Runtime for executing scripts */
82typedef struct ecs_script_runtime_t ecs_script_runtime_t;
83
87typedef struct EcsScript {
88 ecs_script_t *script;
89 ecs_script_template_t *template_; /* Only set for template scripts */
91
93typedef struct ecs_function_ctx_t {
94 ecs_world_t *world;
95 ecs_entity_t function;
96 void *ctx;
98
101 const ecs_function_ctx_t *ctx,
102 int32_t argc,
103 const ecs_value_t *argv,
104 ecs_value_t *result);
105
108 const char *name;
109 ecs_entity_t type;
111
115typedef struct EcsScriptConstVar {
116 ecs_value_t value;
117 const ecs_type_info_t *type_info;
119
123typedef struct EcsScriptFunction {
124 ecs_entity_t return_type;
125 ecs_vec_t params; /* vec<ecs_script_parameter_t> */
127 void *ctx;
129
135typedef struct EcsScriptMethod {
136 ecs_entity_t return_type;
137 ecs_vec_t params; /* vec<ecs_script_parameter_t> */
139 void *ctx;
141
142/* Parsing & running scripts */
143
149
164FLECS_API
166 ecs_world_t *world,
167 const char *name,
168 const char *code,
169 const ecs_script_eval_desc_t *desc);
170
182FLECS_API
184 const ecs_script_t *script,
185 const ecs_script_eval_desc_t *desc);
186
196FLECS_API
199
215FLECS_API
217 ecs_world_t *world,
218 const char *name,
219 const char *code);
220
230FLECS_API
232 ecs_world_t *world,
233 const char *filename);
234
248FLECS_API
249ecs_script_runtime_t* ecs_script_runtime_new(void);
250
256FLECS_API
258 ecs_script_runtime_t *runtime);
259
268FLECS_API
271 ecs_strbuf_t *buf,
272 bool colors);
273
281FLECS_API
284 bool colors);
285
286
287/* Managed scripts (script associated with entity that outlives the function) */
288
290typedef struct ecs_script_desc_t {
291 ecs_entity_t entity; /* Set to customize entity handle associated with script */
292 const char *filename; /* Set to load script from file */
293 const char *code; /* Set to parse script from string */
295
306FLECS_API
308 ecs_world_t *world,
309 const ecs_script_desc_t *desc);
310
311#define ecs_script(world, ...)\
312 ecs_script_init(world, &(ecs_script_desc_t) __VA_ARGS__)
313
321FLECS_API
323 ecs_world_t *world,
325 ecs_entity_t instance,
326 const char *code);
327
334FLECS_API
336 ecs_world_t *world,
338 ecs_entity_t instance);
339
340
341/* Script variables */
342
359FLECS_API
361 ecs_world_t *world);
362
369FLECS_API
371 ecs_script_vars_t *vars);
372
384FLECS_API
386 ecs_script_vars_t *parent);
387
396FLECS_API
398 ecs_script_vars_t *vars);
399
412FLECS_API
414 ecs_script_vars_t *vars,
415 const char *name);
416
432FLECS_API
434 ecs_script_vars_t *vars,
435 const char *name,
436 ecs_entity_t type);
437
438#define ecs_script_vars_define(vars, name, type)\
439 ecs_script_vars_define_id(vars, name, ecs_id(type))
440
450FLECS_API
452 const ecs_script_vars_t *vars,
453 const char *name);
454
487FLECS_API
489 const ecs_iter_t *it,
490 ecs_script_vars_t *vars,
491 int offset);
492
493
494/* Standalone expression evaluation */
495
497typedef struct ecs_expr_eval_desc_t {
498 const char *name;
499 const char *expr;
500 ecs_entity_t (*lookup_action)(
501 const ecs_world_t*,
502 const char *value,
503 void *ctx);
508 /* Disable constant folding (slower evaluation, faster parsing) */
509 bool disable_folding;
510
511 /* Allow for unresolved identifiers when parsing. Useful when entities can
512 * be created in between parsing & evaluating. */
513 bool allow_unresolved_identifiers;
514
515 ecs_script_runtime_t *runtime;
517
532FLECS_API
533const char* ecs_expr_run(
534 ecs_world_t *world,
535 const char *ptr,
536 ecs_value_t *value,
537 const ecs_expr_eval_desc_t *desc);
538
548FLECS_API
550 ecs_world_t *world,
551 const char *expr,
552 const ecs_expr_eval_desc_t *desc);
553
568FLECS_API
570 const ecs_script_t *script,
571 ecs_value_t *value,
572 const ecs_expr_eval_desc_t *desc);
573
586FLECS_API
588 ecs_world_t *world,
589 const char *str,
590 const ecs_script_vars_t *vars);
591
592
593/* Global const variables */
594
596typedef struct ecs_const_var_desc_t {
597 /* Variable name. */
598 const char *name;
599
600 /* Variable parent (namespace). */
601 ecs_entity_t parent;
602
603 /* Variable type. */
604 ecs_entity_t type;
605
606 /* Pointer to value of variable. The value will be copied to an internal
607 * storage and does not need to be kept alive. */
608 void *value;
610
617FLECS_API
619 ecs_world_t *world,
621
622#define ecs_const_var(world, ...)\
623 ecs_const_var_init(world, &(ecs_const_var_desc_t)__VA_ARGS__)
624
625/* Functions */
626
648
656FLECS_API
658 ecs_world_t *world,
659 const ecs_function_desc_t *desc);
660
661#define ecs_function(world, ...)\
662 ecs_function_init(world, &(ecs_function_desc_t)__VA_ARGS__)
663
676FLECS_API
678 ecs_world_t *world,
679 const ecs_function_desc_t *desc);
680
681#define ecs_method(world, ...)\
682 ecs_method_init(world, &(ecs_function_desc_t)__VA_ARGS__)
683
684
685/* Value serialization */
686
696FLECS_API
698 const ecs_world_t *world,
699 ecs_entity_t type,
700 const void *data);
701
711FLECS_API
713 const ecs_world_t *world,
714 ecs_entity_t type,
715 const void *data,
716 ecs_strbuf_t *buf);
717
729FLECS_API
731 const ecs_world_t *world,
732 ecs_entity_t type,
733 const void *data);
734
744FLECS_API
746 const ecs_world_t *world,
747 ecs_entity_t type,
748 const void *data,
749 ecs_strbuf_t *buf);
750
751typedef struct ecs_expr_node_t ecs_expr_node_t;
752
761FLECS_API
763 ecs_world_t *world);
764
765#ifdef __cplusplus
766}
767#endif
768
769#endif
770
773#endif
FLECS_API void ecs_script_runtime_free(ecs_script_runtime_t *runtime)
Free script runtime.
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 ecs_script_t * ecs_expr_parse(ecs_world_t *world, const char *expr, const ecs_expr_eval_desc_t *desc)
Parse expression.
FLECS_API void ecs_script_vars_fini(ecs_script_vars_t *vars)
Free variable scope.
struct ecs_expr_eval_desc_t ecs_expr_eval_desc_t
Used with ecs_expr_run().
FLECS_API ecs_script_runtime_t * ecs_script_runtime_new(void)
Create runtime for script.
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 ecs_entity_t ecs_function_init(ecs_world_t *world, const ecs_function_desc_t *desc)
Create new function.
FLECS_API ecs_script_t * ecs_script_parse(ecs_world_t *world, const char *name, const char *code, const ecs_script_eval_desc_t *desc)
Parse script.
struct EcsScriptConstVar EcsScriptConstVar
Const component.
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 char * ecs_script_ast_to_str(ecs_script_t *script, bool colors)
Convert script AST to string.
void(* ecs_function_callback_t)(const ecs_function_ctx_t *ctx, int32_t argc, const ecs_value_t *argv, ecs_value_t *result)
Script function callback.
Definition script.h:100
FLECS_API ecs_entity_t ecs_script_init(ecs_world_t *world, const ecs_script_desc_t *desc)
Load managed script.
struct ecs_const_var_desc_t ecs_const_var_desc_t
Used with ecs_const_var_init.
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.
FLECS_API int ecs_script_ast_to_buf(ecs_script_t *script, ecs_strbuf_t *buf, bool colors)
Convert script AST to string.
FLECS_API ecs_entity_t ecs_method_init(ecs_world_t *world, const ecs_function_desc_t *desc)
Create new method.
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.
FLECS_API int ecs_script_eval(const ecs_script_t *script, const ecs_script_eval_desc_t *desc)
Evaluate script.
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 int ecs_expr_eval(const ecs_script_t *script, ecs_value_t *value, const ecs_expr_eval_desc_t *desc)
Evaluate expression.
struct ecs_function_desc_t ecs_function_desc_t
Used with ecs_function_init and ecs_method_init.
FLECS_API ecs_script_vars_t * ecs_script_vars_push(ecs_script_vars_t *parent)
Push new variable scope.
struct ecs_script_eval_desc_t ecs_script_eval_desc_t
Used with ecs_script_parse() and ecs_script_eval()
struct ecs_script_vars_t ecs_script_vars_t
Script variable scope.
FLECS_API ecs_entity_t ecs_const_var_init(ecs_world_t *world, ecs_const_var_desc_t *desc)
Create a const variable that can be accessed by scripts.
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.
struct EcsScriptFunction EcsScriptFunction
Function component.
struct ecs_function_ctx_t ecs_function_ctx_t
Script function context.
struct ecs_script_parameter_t ecs_script_parameter_t
Function argument type.
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.
struct EcsScriptMethod EcsScriptMethod
Method component.
FLECS_API const char * ecs_expr_run(ecs_world_t *world, const char *ptr, ecs_value_t *value, const ecs_expr_eval_desc_t *desc)
Run expression.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:346
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:390
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
Const component.
Definition script.h:115
Function component.
Definition script.h:123
Method component.
Definition script.h:135
Script component.
Definition script.h:87
Used with ecs_const_var_init.
Definition script.h:596
Used with ecs_expr_run().
Definition script.h:497
ecs_entity_t type
Type of parsed value (optional)
Definition script.h:506
const ecs_script_vars_t * vars
Variables accessible in expression.
Definition script.h:505
ecs_script_runtime_t * runtime
Reusable runtime (optional)
Definition script.h:515
const char * name
Script name.
Definition script.h:498
const char * expr
Full expression string.
Definition script.h:499
void * lookup_ctx
Context passed to lookup function.
Definition script.h:504
Script function context.
Definition script.h:93
Used with ecs_function_init and ecs_method_init.
Definition script.h:628
ecs_entity_t return_type
Function return type.
Definition script.h:640
const char * name
Function name.
Definition script.h:630
ecs_script_parameter_t params[(16)]
Function parameters.
Definition script.h:637
void * ctx
Context passed to function implementation.
Definition script.h:646
ecs_function_callback_t callback
Function implementation.
Definition script.h:643
ecs_entity_t parent
Parent of function.
Definition script.h:634
Iterator.
Definition flecs.h:1100
Used with ecs_script_init()
Definition script.h:290
Used with ecs_script_parse() and ecs_script_eval()
Definition script.h:145
ecs_script_runtime_t * runtime
Reusable runtime (optional)
Definition script.h:147
ecs_script_vars_t * vars
Variables used by script.
Definition script.h:146
Function argument type.
Definition script.h:107
Script object.
Definition script.h:75
Script variable.
Definition script.h:55
Script variable scope.
Definition script.h:63
Type that contains component information (passed to ctors/dtors/...)
Definition flecs.h:954
Utility to hold a value of a dynamic type.
Definition flecs.h:967