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 int32_t sp;
60 bool is_const;
62
64typedef struct ecs_script_vars_t {
65 struct ecs_script_vars_t *parent;
66 int32_t sp;
67
68 ecs_hashmap_t var_index;
69 ecs_vec_t vars;
70
71 const ecs_world_t *world;
72 struct ecs_stack_t *stack;
73 ecs_stack_cursor_t *cursor;
74 ecs_allocator_t *allocator;
76
78typedef struct ecs_script_t {
79 ecs_world_t *world;
80 const char *name;
81 const char *code;
83
84/* Runtime for executing scripts */
85typedef struct ecs_script_runtime_t ecs_script_runtime_t;
86
90typedef struct EcsScript {
91 ecs_script_t *script;
92 ecs_script_template_t *template_; /* Only set for template scripts */
94
96typedef struct ecs_function_ctx_t {
97 ecs_world_t *world;
98 ecs_entity_t function;
99 void *ctx;
101
104 const ecs_function_ctx_t *ctx,
105 int32_t argc,
106 const ecs_value_t *argv,
107 ecs_value_t *result);
108
111 const char *name;
112 ecs_entity_t type;
114
118typedef struct EcsScriptConstVar {
119 ecs_value_t value;
120 const ecs_type_info_t *type_info;
122
126typedef struct EcsScriptFunction {
127 ecs_entity_t return_type;
128 ecs_vec_t params; /* vec<ecs_script_parameter_t> */
130 void *ctx;
132
138typedef struct EcsScriptMethod {
139 ecs_entity_t return_type;
140 ecs_vec_t params; /* vec<ecs_script_parameter_t> */
142 void *ctx;
144
145/* Parsing & running scripts */
146
152
167FLECS_API
169 ecs_world_t *world,
170 const char *name,
171 const char *code,
172 const ecs_script_eval_desc_t *desc);
173
185FLECS_API
187 const ecs_script_t *script,
188 const ecs_script_eval_desc_t *desc);
189
199FLECS_API
202
218FLECS_API
220 ecs_world_t *world,
221 const char *name,
222 const char *code);
223
233FLECS_API
235 ecs_world_t *world,
236 const char *filename);
237
251FLECS_API
252ecs_script_runtime_t* ecs_script_runtime_new(void);
253
259FLECS_API
261 ecs_script_runtime_t *runtime);
262
271FLECS_API
274 ecs_strbuf_t *buf,
275 bool colors);
276
284FLECS_API
287 bool colors);
288
289
290/* Managed scripts (script associated with entity that outlives the function) */
291
293typedef struct ecs_script_desc_t {
294 ecs_entity_t entity; /* Set to customize entity handle associated with script */
295 const char *filename; /* Set to load script from file */
296 const char *code; /* Set to parse script from string */
298
309FLECS_API
311 ecs_world_t *world,
312 const ecs_script_desc_t *desc);
313
314#define ecs_script(world, ...)\
315 ecs_script_init(world, &(ecs_script_desc_t) __VA_ARGS__)
316
324FLECS_API
326 ecs_world_t *world,
328 ecs_entity_t instance,
329 const char *code);
330
337FLECS_API
339 ecs_world_t *world,
341 ecs_entity_t instance);
342
343
344/* Script variables */
345
362FLECS_API
364 ecs_world_t *world);
365
372FLECS_API
374 ecs_script_vars_t *vars);
375
387FLECS_API
389 ecs_script_vars_t *parent);
390
399FLECS_API
401 ecs_script_vars_t *vars);
402
415FLECS_API
417 ecs_script_vars_t *vars,
418 const char *name);
419
435FLECS_API
437 ecs_script_vars_t *vars,
438 const char *name,
439 ecs_entity_t type);
440
441#define ecs_script_vars_define(vars, name, type)\
442 ecs_script_vars_define_id(vars, name, ecs_id(type))
443
453FLECS_API
455 const ecs_script_vars_t *vars,
456 const char *name);
457
470FLECS_API
472 const ecs_script_vars_t *vars,
473 int32_t sp);
474
480FLECS_API
482 const ecs_script_vars_t *vars);
483
492FLECS_API
494 ecs_script_vars_t *vars,
495 int32_t count);
496
529FLECS_API
531 const ecs_iter_t *it,
532 ecs_script_vars_t *vars,
533 int offset);
534
535
536/* Standalone expression evaluation */
537
539typedef struct ecs_expr_eval_desc_t {
540 const char *name;
541 const char *expr;
544 ecs_entity_t (*lookup_action)(
545 const ecs_world_t*,
546 const char *value,
547 void *ctx);
552
557
561
562 ecs_script_runtime_t *runtime;
564
579FLECS_API
580const char* ecs_expr_run(
581 ecs_world_t *world,
582 const char *ptr,
583 ecs_value_t *value,
584 const ecs_expr_eval_desc_t *desc);
585
595FLECS_API
597 ecs_world_t *world,
598 const char *expr,
599 const ecs_expr_eval_desc_t *desc);
600
615FLECS_API
617 const ecs_script_t *script,
618 ecs_value_t *value,
619 const ecs_expr_eval_desc_t *desc);
620
633FLECS_API
635 ecs_world_t *world,
636 const char *str,
637 const ecs_script_vars_t *vars);
638
639
640/* Global const variables */
641
643typedef struct ecs_const_var_desc_t {
644 /* Variable name. */
645 const char *name;
646
647 /* Variable parent (namespace). */
648 ecs_entity_t parent;
649
650 /* Variable type. */
651 ecs_entity_t type;
652
653 /* Pointer to value of variable. The value will be copied to an internal
654 * storage and does not need to be kept alive. */
655 void *value;
657
664FLECS_API
666 ecs_world_t *world,
668
669#define ecs_const_var(world, ...)\
670 ecs_const_var_init(world, &(ecs_const_var_desc_t)__VA_ARGS__)
671
672/* Functions */
673
695
703FLECS_API
705 ecs_world_t *world,
706 const ecs_function_desc_t *desc);
707
708#define ecs_function(world, ...)\
709 ecs_function_init(world, &(ecs_function_desc_t)__VA_ARGS__)
710
723FLECS_API
725 ecs_world_t *world,
726 const ecs_function_desc_t *desc);
727
728#define ecs_method(world, ...)\
729 ecs_method_init(world, &(ecs_function_desc_t)__VA_ARGS__)
730
731
732/* Value serialization */
733
743FLECS_API
745 const ecs_world_t *world,
746 ecs_entity_t type,
747 const void *data);
748
758FLECS_API
760 const ecs_world_t *world,
761 ecs_entity_t type,
762 const void *data,
763 ecs_strbuf_t *buf);
764
776FLECS_API
778 const ecs_world_t *world,
779 ecs_entity_t type,
780 const void *data);
781
791FLECS_API
793 const ecs_world_t *world,
794 ecs_entity_t type,
795 const void *data,
796 ecs_strbuf_t *buf);
797
798typedef struct ecs_expr_node_t ecs_expr_node_t;
799
808FLECS_API
810 ecs_world_t *world);
811
812#ifdef __cplusplus
813}
814#endif
815
816#endif
817
820#endif
FLECS_API void ecs_script_runtime_free(ecs_script_runtime_t *runtime)
Free script runtime.
FLECS_API void ecs_script_vars_print(const ecs_script_vars_t *vars)
Print variables.
FLECS_API void ecs_script_vars_set_size(ecs_script_vars_t *vars, int32_t count)
Preallocate space for variables.
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:103
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 ecs_script_var_t * ecs_script_vars_from_sp(const ecs_script_vars_t *vars, int32_t sp)
Lookup a variable by stack pointer.
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:118
Function component.
Definition script.h:126
Method component.
Definition script.h:138
Script component.
Definition script.h:90
Used with ecs_const_var_init.
Definition script.h:643
Used with ecs_expr_run().
Definition script.h:539
bool disable_folding
Disable constant folding (slower evaluation, faster parsing)
Definition script.h:551
ecs_entity_t type
Type of parsed value (optional)
Definition script.h:543
const ecs_script_vars_t * vars
Variables accessible in expression.
Definition script.h:542
ecs_script_runtime_t * runtime
Reusable runtime (optional)
Definition script.h:562
bool allow_unresolved_identifiers
Allow for unresolved identifiers when parsing.
Definition script.h:560
const char * name
Script name.
Definition script.h:540
const char * expr
Full expression string.
Definition script.h:541
bool disable_dynamic_variable_binding
This option instructs the expression runtime to lookup variables by stack pointer instead of by name,...
Definition script.h:556
void * lookup_ctx
Context passed to lookup function.
Definition script.h:548
Script function context.
Definition script.h:96
Used with ecs_function_init and ecs_method_init.
Definition script.h:675
ecs_entity_t return_type
Function return type.
Definition script.h:687
const char * name
Function name.
Definition script.h:677
ecs_script_parameter_t params[(16)]
Function parameters.
Definition script.h:684
void * ctx
Context passed to function implementation.
Definition script.h:693
ecs_function_callback_t callback
Function implementation.
Definition script.h:690
ecs_entity_t parent
Parent of function.
Definition script.h:681
Iterator.
Definition flecs.h:1099
Used with ecs_script_init()
Definition script.h:293
Used with ecs_script_parse() and ecs_script_eval()
Definition script.h:148
ecs_script_runtime_t * runtime
Reusable runtime (optional)
Definition script.h:150
ecs_script_vars_t * vars
Variables used by script.
Definition script.h:149
Function argument type.
Definition script.h:110
Script object.
Definition script.h:78
Script variable.
Definition script.h:55
Script variable scope.
Definition script.h:64
Type that contains component information (passed to ctors/dtors/...)
Definition flecs.h:953
Utility to hold a value of a dynamic type.
Definition flecs.h:966