Skip to content
Flecs v4.1
Flecs script

DSL for loading scenes, assets, and configuration. More...

Classes

struct  ecs_script_var_t
 Script variable. More...
struct  ecs_script_vars_t
 Script variable scope. More...
struct  ecs_script_t
 Script object. More...
struct  EcsScript
 Script component. More...
struct  ecs_function_ctx_t
 Script function context. More...
struct  ecs_script_parameter_t
 Function argument type. More...
struct  ecs_script_eval_desc_t
 Used with ecs_script_parse() and ecs_script_eval(). More...
struct  ecs_script_eval_result_t
 Used to capture error output from script evaluation. More...
struct  EcsScriptConstVar
 Const component. More...
struct  EcsScriptMutVar
 Mut component. More...
struct  ecs_script_function_t
struct  ecs_script_source_t
 Source location of a script statement. More...
struct  ecs_script_desc_t
 Used with ecs_script_init(). More...
struct  ecs_expr_eval_desc_t
 Used with ecs_expr_run(). More...
struct  ecs_const_var_desc_t
 Used with ecs_const_var_init(). More...
struct  ecs_mut_var_desc_t
 Used with ecs_mut_var_init(). More...
struct  ecs_vector_fn_callbacks_t
 Vector function callbacks for different element types. More...
struct  ecs_function_desc_t
 Used with ecs_function_init() and ecs_method_init(). More...

Macros

#define FLECS_SCRIPT_H
#define FLECS_SCRIPT_FUNCTION_ARGS_MAX   (16)
#define FLECS_SCRIPT_VECTOR_FUNCTION_COUNT   (18)
#define ecs_script(world, ...)
#define ecs_script_vars_define(vars, name, type)
#define ecs_const_var(world, ...)
#define ecs_const_var_get_t(world, name, T)
 Return pointer to the value of a const variable.
#define ecs_mut_var(world, ...)
#define ecs_mut_var_get_t(world, name, T)
 Return pointer to the value of a mut variable.
#define ecs_mut_var_set_t(world, name, T, ...)
 Set the value of a mut variable.
#define ecs_function(world, ...)
#define ecs_method(world, ...)

Typedefs

typedef struct ecs_script_template_t ecs_script_template_t
typedef struct ecs_script_var_t ecs_script_var_t
 Script variable.
typedef struct ecs_script_vars_t ecs_script_vars_t
 Script variable scope.
typedef struct ecs_script_t ecs_script_t
 Script object.
typedef struct ecs_script_runtime_t ecs_script_runtime_t
 Runtime for executing scripts.
typedef struct EcsScript EcsScript
 Script component.
typedef struct ecs_function_ctx_t ecs_function_ctx_t
 Script function context.
typedef 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.
typedef void(*) ecs_vector_function_callback_t(const ecs_function_ctx_t *ctx, int32_t argc, const ecs_value_t *argv, ecs_value_t *result, int32_t elem_count)
 Script vector function callback.
typedef struct ecs_script_parameter_t ecs_script_parameter_t
 Function argument type.
typedef struct ecs_script_eval_desc_t ecs_script_eval_desc_t
 Used with ecs_script_parse() and ecs_script_eval().
typedef struct ecs_script_eval_result_t ecs_script_eval_result_t
 Used to capture error output from script evaluation.
typedef struct EcsScriptConstVar EcsScriptConstVar
 Const component.
typedef struct EcsScriptMutVar EcsScriptMutVar
 Mut component.
typedef struct ecs_script_function_t EcsScriptFunction
 Function component.
typedef struct ecs_script_function_t EcsScriptMethod
 Method component.
typedef struct ecs_script_source_t ecs_script_source_t
 Source location of a script statement.
typedef struct ecs_script_edits_t ecs_script_edits_t
 Set of pending edits for a script.
typedef struct ecs_script_desc_t ecs_script_desc_t
 Used with ecs_script_init().
typedef struct ecs_expr_eval_desc_t ecs_expr_eval_desc_t
 Used with ecs_expr_run().
typedef struct ecs_const_var_desc_t ecs_const_var_desc_t
 Used with ecs_const_var_init().
typedef struct ecs_mut_var_desc_t ecs_mut_var_desc_t
 Used with ecs_mut_var_init().
typedef struct ecs_vector_fn_callbacks_t ecs_vector_fn_callbacks_t
 Vector function callbacks for different element types.
typedef struct ecs_function_desc_t ecs_function_desc_t
 Used with ecs_function_init() and ecs_method_init().
typedef struct ecs_expr_node_t ecs_expr_node_t

Functions

FLECS_API ECS_COMPONENT_DECLARE (EcsScript)
FLECS_API ECS_DECLARE (EcsScriptTemplate)
FLECS_API ECS_DECLARE (EcsScriptTemplateManual)
FLECS_API ECS_DECLARE (EcsScriptTemplatePending)
FLECS_API int ecs_script_template_update (ecs_world_t *world, ecs_entity_t instance, ecs_entity_t template_entity)
FLECS_API ECS_COMPONENT_DECLARE (EcsScriptConstVar)
FLECS_API ECS_COMPONENT_DECLARE (EcsScriptMutVar)
FLECS_API ECS_COMPONENT_DECLARE (EcsScriptFunction)
FLECS_API ECS_DECLARE (EcsScriptVectorType)
FLECS_API ecs_script_tecs_script_parse (ecs_world_t *world, const char *name, const char *code, const ecs_script_eval_desc_t *desc, ecs_script_eval_result_t *result)
 Parse script.
FLECS_API int ecs_script_eval (const ecs_script_t *script, const ecs_script_eval_desc_t *desc, ecs_script_eval_result_t *result)
 Evaluate script.
FLECS_API void ecs_script_free (ecs_script_t *script)
 Free script.
FLECS_API int ecs_script_run (ecs_world_t *world, const char *name, const char *code, ecs_script_eval_result_t *result)
 Parse script.
FLECS_API int ecs_script_run_w_desc (ecs_world_t *world, const char *name, const char *code, const ecs_script_eval_desc_t *desc, ecs_script_eval_result_t *result)
 Parse script and evaluate with options.
FLECS_API int ecs_script_run_file (ecs_world_t *world, const char *filename)
 Parse script file.
FLECS_API int ecs_script_run_file_w_desc (ecs_world_t *world, const char *filename, const ecs_script_eval_desc_t *desc)
 Parse script file and evaluate with options.
FLECS_API char * ecs_script_ir_to_str (const ecs_script_t *script)
 Convert script IR to string.
FLECS_API ecs_script_runtime_tecs_script_runtime_new (void)
 Create runtime for script.
FLECS_API void ecs_script_runtime_free (ecs_script_runtime_t *runtime)
 Free script runtime.
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 char * ecs_script_ast_to_str (ecs_script_t *script, bool colors)
 Convert script AST to string.
FLECS_API bool ecs_script_entity_source (const ecs_script_t *script, ecs_entity_t entity, ecs_script_source_t *source)
 Find the statement that declares an entity.
FLECS_API ecs_entity_t ecs_script_entity_owner (const ecs_world_t *world, ecs_entity_t entity)
 Find the managed script that declares an entity.
FLECS_API ecs_script_edits_tecs_script_edits_new (ecs_script_t *script)
 Create an edit set for a script.
FLECS_API void ecs_script_edits_free (ecs_script_edits_t *edits)
 Free an edit set.
FLECS_API int ecs_script_edits_set (ecs_script_edits_t *edits, ecs_entity_t entity, ecs_id_t component, const void *value)
 Set a component value on an entity.
FLECS_API int ecs_script_edits_set_expr (ecs_script_edits_t *edits, ecs_entity_t entity, ecs_id_t component, const char *expr)
 Set a component value on an entity from an expression string.
FLECS_API int ecs_script_edits_remove (ecs_script_edits_t *edits, ecs_entity_t entity, ecs_id_t component)
 Remove a component from an entity.
FLECS_API int ecs_script_edits_delete (ecs_script_edits_t *edits, ecs_entity_t entity)
 Delete an entity.
FLECS_API int ecs_script_edits_clear (ecs_script_edits_t *edits, ecs_entity_t entity, ecs_id_t component)
 Remove a recorded edit from an edit set.
FLECS_API int32_t ecs_script_edits_count (const ecs_script_edits_t *edits)
 Return the number of recorded edits in an edit set.
FLECS_API char * ecs_script_edits_apply (const ecs_script_edits_t *edits)
 Apply an edit set.
FLECS_API ecs_entity_t ecs_script_init (ecs_world_t *world, const ecs_script_desc_t *desc)
 Load managed script.
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 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_vars_tecs_script_vars_init (ecs_world_t *world)
 Create new variable scope.
FLECS_API void ecs_script_vars_fini (ecs_script_vars_t *vars)
 Free variable scope.
FLECS_API ecs_script_vars_tecs_script_vars_push (ecs_script_vars_t *parent)
 Push new variable scope.
FLECS_API ecs_script_vars_tecs_script_vars_pop (ecs_script_vars_t *vars)
 Pop variable scope.
FLECS_API ecs_script_var_tecs_script_vars_declare (ecs_script_vars_t *vars, const char *name)
 Declare a variable.
FLECS_API ecs_script_var_tecs_script_vars_define_id (ecs_script_vars_t *vars, const char *name, ecs_entity_t type)
 Define a variable.
FLECS_API ecs_script_var_tecs_script_vars_lookup (const ecs_script_vars_t *vars, const char *name)
 Lookup a variable.
FLECS_API ecs_script_var_tecs_script_vars_from_sp (const ecs_script_vars_t *vars, int32_t sp)
 Lookup a variable by stack pointer.
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 void ecs_script_vars_from_iter (const ecs_iter_t *it, ecs_script_vars_t *vars, int offset)
 Convert iterator to vars.
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.
FLECS_API ecs_script_tecs_expr_parse (ecs_world_t *world, const char *expr, const ecs_expr_eval_desc_t *desc)
 Parse expression.
FLECS_API int ecs_expr_eval (const ecs_script_t *script, ecs_value_t *value, const ecs_expr_eval_desc_t *desc)
 Evaluate expression.
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_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 ecs_value_t ecs_const_var_get (const ecs_world_t *world, ecs_entity_t var)
 Return the value for a const variable.
FLECS_API void * ecs_const_var_get_w_type (const ecs_world_t *world, const char *name, ecs_entity_t type, ecs_size_t size, void *out)
 Return pointer to the value of a const variable.
FLECS_API ecs_entity_t ecs_mut_var_init (ecs_world_t *world, ecs_mut_var_desc_t *desc)
 Create a mut variable that can be accessed by scripts.
FLECS_API ecs_value_t ecs_mut_var_get (const ecs_world_t *world, ecs_entity_t var)
 Return the value for a mut variable.
FLECS_API void * ecs_mut_var_get_w_type (const ecs_world_t *world, const char *name, ecs_entity_t type, ecs_size_t size, void *out)
 Return pointer to the value of a mut variable.
FLECS_API int ecs_mut_var_set_w_type (ecs_world_t *world, const char *name, ecs_entity_t type, ecs_size_t size, const void *value)
 Set the value of a mut variable.
FLECS_API void ecs_mut_var_modified (ecs_world_t *world, ecs_entity_t var)
 Mark mut var as modified.
FLECS_API ecs_entity_t ecs_function_init (ecs_world_t *world, const ecs_function_desc_t *desc)
 Create new function.
FLECS_API int ecs_function_call (ecs_world_t *world, ecs_entity_t function, int32_t argc, const ecs_value_t *argv, ecs_value_t *result)
FLECS_API ecs_entity_t ecs_method_init (ecs_world_t *world, const ecs_function_desc_t *desc)
 Create new method.
FLECS_API int ecs_method_call (ecs_world_t *world, ecs_entity_t method, const ecs_value_t *instance, int32_t argc, const ecs_value_t *argv, ecs_value_t *result)
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 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 char * ecs_ptr_to_str (const ecs_world_t *world, ecs_entity_t type, const void *data)
 Similar to ecs_ptr_to_expr(), but serializes values to string.
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 void FlecsScriptImport (ecs_world_t *world)
 Script module import function.

Detailed Description

DSL for loading scenes, assets, and configuration.

Macro Definition Documentation

◆ ecs_const_var

#define ecs_const_var ( world,
... )
Value:
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.
Used with ecs_const_var_init().
Definition script.h:1141

Definition at line 1167 of file script.h.

◆ ecs_const_var_get_t

#define ecs_const_var_get_t ( world,
name,
T )
Value:
(*ECS_CAST(T*, ecs_const_var_get_w_type(\
world, name, ecs_id(T), ECS_SIZEOF(T), &(T){0})))
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
FLECS_API void * ecs_const_var_get_w_type(const ecs_world_t *world, const char *name, ecs_entity_t type, ecs_size_t size, void *out)
Return pointer to the value of a const variable.

Return pointer to the value of a const variable.

This operation returns the value of a const variable, casted to the specified type. If the type is equal to that of the const variable, no cast is performed. If the variable cannot be casted to the specified type, the operation will throw an error.

The returned value is owned by the caller. If the returned value contains allocated memory, this needs to be freed by the caller.

When the operation fails, a zero initialized value is returned.

Parameters
worldThe world.
nameThe name of the const variable.
TThe requested type.
Returns
The value of the const variable.

Definition at line 1226 of file script.h.

◆ ecs_function

#define ecs_function ( world,
... )
Value:
FLECS_API ecs_entity_t ecs_function_init(ecs_world_t *world, const ecs_function_desc_t *desc)
Create new function.
Used with ecs_function_init() and ecs_method_init().
Definition script.h:1396

Definition at line 1478 of file script.h.

◆ ecs_method

#define ecs_method ( world,
... )
Value:
ecs_method_init(world, &(ecs_function_desc_t)__VA_ARGS__)
FLECS_API ecs_entity_t ecs_method_init(ecs_world_t *world, const ecs_function_desc_t *desc)
Create new method.

Definition at line 1506 of file script.h.

◆ ecs_mut_var

#define ecs_mut_var ( world,
... )
Value:
ecs_mut_var_init(world, &(ecs_mut_var_desc_t)__VA_ARGS__)
FLECS_API ecs_entity_t ecs_mut_var_init(ecs_world_t *world, ecs_mut_var_desc_t *desc)
Create a mut variable that can be accessed by scripts.
Used with ecs_mut_var_init().
Definition script.h:1234

Definition at line 1263 of file script.h.

◆ ecs_mut_var_get_t

#define ecs_mut_var_get_t ( world,
name,
T )
Value:
(*ECS_CAST(T*, ecs_mut_var_get_w_type(\
world, name, ecs_id(T), ECS_SIZEOF(T), &(T){0})))
FLECS_API void * ecs_mut_var_get_w_type(const ecs_world_t *world, const char *name, ecs_entity_t type, ecs_size_t size, void *out)
Return pointer to the value of a mut variable.

Return pointer to the value of a mut variable.

This operation returns the value of a mut variable, casted to the specified type. If the type is equal to that of the mut variable, no cast is performed. If the variable cannot be casted to the specified type, the operation will throw an error.

The returned value is owned by the caller. If the returned value contains allocated memory, this needs to be freed by the caller.

When the operation fails, a zero initialized value is returned.

Parameters
worldThe world.
nameThe name of the mut variable.
TThe requested type.
Returns
The value of the mut variable.

Definition at line 1322 of file script.h.

◆ ecs_mut_var_set_t

#define ecs_mut_var_set_t ( world,
name,
T,
... )
Value:
world, name, ecs_id(T), ECS_SIZEOF(T), &(T)__VA_ARGS__)
FLECS_API int ecs_mut_var_set_w_type(ecs_world_t *world, const char *name, ecs_entity_t type, ecs_size_t size, const void *value)
Set the value of a mut variable.

Set the value of a mut variable.

This operation sets the value of a mut variable from a value of the specified type. If the type is equal to that of the mut variable, no cast is performed. If the value cannot be casted to the type of the mut variable, the operation will throw an error.

The provided value is copied into the storage of the mut variable and does not need to be kept alive.

On success, OnSet observers for the mut variable are notified, which causes scripts that use the variable to be reevaluated.

Parameters
worldThe world.
nameThe name of the mut variable.
TThe type of the provided value.
Returns
Zero if success, non-zero if failed.

Definition at line 1372 of file script.h.

◆ ecs_script

#define ecs_script ( world,
... )
Value:
ecs_script_init(world, &(ecs_script_desc_t) __VA_ARGS__)
FLECS_API ecs_entity_t ecs_script_init(ecs_world_t *world, const ecs_script_desc_t *desc)
Load managed script.
Used with ecs_script_init().
Definition script.h:783

Definition at line 807 of file script.h.

◆ ecs_script_vars_define

#define ecs_script_vars_define ( vars,
name,
type )
Value:
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.

Definition at line 936 of file script.h.

◆ FLECS_SCRIPT_FUNCTION_ARGS_MAX

#define FLECS_SCRIPT_FUNCTION_ARGS_MAX   (16)

Definition at line 37 of file script.h.

◆ FLECS_SCRIPT_H

#define FLECS_SCRIPT_H

Definition at line 31 of file script.h.

◆ FLECS_SCRIPT_VECTOR_FUNCTION_COUNT

#define FLECS_SCRIPT_VECTOR_FUNCTION_COUNT   (18)

Definition at line 40 of file script.h.

Typedef Documentation

◆ ecs_expr_node_t

typedef struct ecs_expr_node_t ecs_expr_node_t

Definition at line 1585 of file script.h.

◆ ecs_function_callback_t

typedef 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 at line 143 of file script.h.

◆ ecs_script_edits_t

Set of pending edits for a script.

See ecs_script_edits_new().

Definition at line 558 of file script.h.

◆ ecs_script_runtime_t

Runtime for executing scripts.

Definition at line 117 of file script.h.

◆ ecs_script_source_t

typedef struct ecs_script_source_t ecs_script_source_t

Source location of a script statement.

Returned by ecs_script_entity_source(). The offset and length are byte offsets into ecs_script_t::code.

◆ ecs_script_template_t

typedef struct ecs_script_template_t ecs_script_template_t

Definition at line 83 of file script.h.

◆ ecs_vector_function_callback_t

typedef void(*) ecs_vector_function_callback_t(const ecs_function_ctx_t *ctx, int32_t argc, const ecs_value_t *argv, ecs_value_t *result, int32_t elem_count)

Script vector function callback.

Definition at line 150 of file script.h.

◆ EcsScript

typedef struct EcsScript EcsScript

Script component.

This component is added to the entities of managed scripts and templates.

◆ EcsScriptConstVar

typedef struct EcsScriptConstVar EcsScriptConstVar

Const component.

This component describes a const variable that can be used from scripts. The value of a const variable is folded into expressions that use it.

◆ EcsScriptFunction

Function component.

This component describes a function that can be called from a script.

Definition at line 243 of file script.h.

◆ EcsScriptMethod

Method component.

This component describes a method that can be called from a script. Methods are functions that can be called on instances of a type. A method entity is stored in the scope of the type it belongs to.

Definition at line 250 of file script.h.

◆ EcsScriptMutVar

typedef struct EcsScriptMutVar EcsScriptMutVar

Mut component.

This component describes a mutable global variable that can be used from scripts. Unlike a const variable, the value of a mut variable is never folded into expressions, and scripts that use it are reevaluated when it changes.

Function Documentation

◆ ecs_const_var_get()

FLECS_API ecs_value_t ecs_const_var_get ( const ecs_world_t * world,
ecs_entity_t var )

Return the value for a const variable.

This returns the value for a const variable that is created either with ecs_const_var_init(), or in a script with "export const v = ...".

Parameters
worldThe world.
varThe const variable.
Returns
The value of the const variable.

◆ ecs_const_var_get_w_type()

FLECS_API void * ecs_const_var_get_w_type ( const ecs_world_t * world,
const char * name,
ecs_entity_t type,
ecs_size_t size,
void * out )

Return pointer to the value of a const variable.

This operation returns the value of a const variable, casted to the specified type. If the type is equal to that of the const variable, no cast is performed. If the variable cannot be casted to the specified type, the operation will throw an error.

The returned value is owned by the caller. If the returned value contains allocated memory, this needs to be freed by the caller.

This operation is intended to be used by the ecs_const_var_get_t macro.

Parameters
worldThe world.
nameThe name of the const variable.
typeThe requested type.
sizeThe size of the requested type.
outStorage for the value of the const variable.
Returns
Pointer to the value of the const variable.

◆ ecs_const_var_init()

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.

Parameters
worldThe world.
descConst var parameters.
Returns
The const var, or 0 if failed.

◆ ecs_expr_eval()

FLECS_API int ecs_expr_eval ( const ecs_script_t * script,
ecs_value_t * value,
const ecs_expr_eval_desc_t * desc )

Evaluate expression.

This operation evaluates an expression parsed with ecs_expr_parse() and stores the result in the provided value. If the value contains a type that is different from the type of the expression, the expression will be cast to the value.

If the provided value for value.ptr is NULL, the value must be freed with ecs_ptr_free() afterwards.

Parameters
scriptThe script containing the expression.
valueThe value in which to store the expression result.
descConfiguration parameters for the parser.
Returns
Zero if successful, non-zero if failed.

◆ ecs_expr_parse()

FLECS_API ecs_script_t * ecs_expr_parse ( ecs_world_t * world,
const char * expr,
const ecs_expr_eval_desc_t * desc )

Parse expression.

This operation parses an expression and returns an object that can be evaluated multiple times with ecs_expr_eval().

Parameters
worldThe world.
exprThe expression string.
descConfiguration parameters for the parser.
Returns
A script object if parsing is successful, NULL if parsing failed.

◆ ecs_expr_run()

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.

This operation runs an expression and stores the result in the provided value. If the value contains a type that is different from the type of the expression, the expression will be cast to the value.

If the provided value for value.ptr is NULL, the value must be freed with ecs_ptr_free() afterwards.

Parameters
worldThe world.
ptrThe pointer to the expression to parse.
valueThe value containing type and pointer to write to.
descConfiguration parameters for the parser.
Returns
Pointer to the character after the last one read, or NULL if failed.

◆ ecs_function_init()

FLECS_API ecs_entity_t ecs_function_init ( ecs_world_t * world,
const ecs_function_desc_t * desc )

Create new function.

This operation creates a new function that can be called from a script.

Parameters
worldThe world.
descFunction init parameters.
Returns
The function, or 0 if failed.

◆ ecs_method_init()

FLECS_API ecs_entity_t ecs_method_init ( ecs_world_t * world,
const ecs_function_desc_t * desc )

Create new method.

This operation creates a new method that can be called from a script. A method is like a function, except that it can be called on every instance of a type.

Methods automatically receive the instance on which the method is invoked as first argument.

Parameters
worldThe world.
descMethod init parameters.
Returns
The method, or 0 if failed.

◆ ecs_mut_var_get()

FLECS_API ecs_value_t ecs_mut_var_get ( const ecs_world_t * world,
ecs_entity_t var )

Return the value for a mut variable.

This returns the value for a mut variable that is created either with ecs_mut_var_init(), or in a script with "export mut v = ...".

Parameters
worldThe world.
varThe mut variable.
Returns
The value of the mut variable.

◆ ecs_mut_var_get_w_type()

FLECS_API void * ecs_mut_var_get_w_type ( const ecs_world_t * world,
const char * name,
ecs_entity_t type,
ecs_size_t size,
void * out )

Return pointer to the value of a mut variable.

This operation returns the value of a mut variable, casted to the specified type. If the type is equal to that of the mut variable, no cast is performed. If the variable cannot be casted to the specified type, the operation will throw an error.

The returned value is owned by the caller. If the returned value contains allocated memory, this needs to be freed by the caller.

This operation is intended to be used by the ecs_mut_var_get_t macro.

Parameters
worldThe world.
nameThe name of the mut variable.
typeThe requested type.
sizeThe size of the requested type.
outStorage for the value of the mut variable.
Returns
Pointer to the value of the mut variable.

◆ ecs_mut_var_init()

FLECS_API ecs_entity_t ecs_mut_var_init ( ecs_world_t * world,
ecs_mut_var_desc_t * desc )

Create a mut variable that can be accessed by scripts.

Unlike a const variable the value of a mut variable is never folded into expressions, which means scripts that use it are reevaluated when the value changes.

Parameters
worldThe world.
descMut var parameters.
Returns
The mut var, or 0 if failed.

◆ ecs_mut_var_modified()

FLECS_API void ecs_mut_var_modified ( ecs_world_t * world,
ecs_entity_t var )

Mark mut var as modified.

This will notify OnSet observers.

Parameters
worldThe world.
varThe mut variable.

◆ ecs_mut_var_set_w_type()

FLECS_API int ecs_mut_var_set_w_type ( ecs_world_t * world,
const char * name,
ecs_entity_t type,
ecs_size_t size,
const void * value )

Set the value of a mut variable.

This operation sets the value of a mut variable from a value of the specified type. If the type is equal to that of the mut variable, no cast is performed. If the value cannot be casted to the type of the mut variable, the operation will throw an error.

The provided value is copied into the storage of the mut variable and does not need to be kept alive.

On success, OnSet observers for the mut variable are notified, which causes scripts that use the variable to be reevaluated.

This operation is intended to be used by the ecs_mut_var_set_t macro.

Parameters
worldThe world.
nameThe name of the mut variable.
typeThe type of the provided value.
sizeThe size of the provided type.
valuePointer to the value to set.
Returns
Zero if success, non-zero if failed.

◆ 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.

This operation serializes a value of the provided type to a string. The memory pointed to must be large enough to contain a value of the used type.

Parameters
worldThe world.
typeThe type of the value to serialize.
dataThe value to serialize.
Returns
String with expression, or NULL if failed.

◆ 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.

Same as ecs_ptr_to_expr(), but serializes to an ecs_strbuf_t instance.

Parameters
worldThe world.
typeThe type of the value to serialize.
dataThe value to serialize.
bufThe strbuf to append the string to.
Returns
Zero if success, non-zero if failed.

◆ ecs_ptr_to_str()

FLECS_API char * ecs_ptr_to_str ( const ecs_world_t * world,
ecs_entity_t type,
const void * data )

Similar to ecs_ptr_to_expr(), but serializes values to string.

Whereas the output of ecs_ptr_to_expr() is a valid expression, the output of ecs_ptr_to_str() is a string representation of the value. In most cases the output of the two operations is the same, but there are some differences:

  • Strings are not quoted
Parameters
worldThe world.
typeThe type of the value to serialize.
dataThe value to serialize.
Returns
String with result, or NULL if failed.

◆ 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.

Same as ecs_ptr_to_str(), but serializes to an ecs_strbuf_t instance.

Parameters
worldThe world.
typeThe type of the value to serialize.
dataThe value to serialize.
bufThe strbuf to append the string to.
Returns
Zero if success, non-zero if failed.

◆ ecs_script_ast_to_buf()

FLECS_API int ecs_script_ast_to_buf ( ecs_script_t * script,
ecs_strbuf_t * buf,
bool colors )

Convert script AST to string.

This operation converts the script abstract syntax tree to a string, which can be used to debug a script.

Parameters
scriptThe script.
bufThe buffer to write to.
colorsWhether to include ANSI color codes in the output.
Returns
Zero if success, non-zero if failed.

◆ ecs_script_ast_to_str()

FLECS_API char * ecs_script_ast_to_str ( ecs_script_t * script,
bool colors )

Convert script AST to string.

This operation converts the script abstract syntax tree to a string, which can be used to debug a script.

Parameters
scriptThe script.
colorsWhether to include ANSI color codes in the output.
Returns
The string if success, NULL if failed.

◆ 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.

Parameters
worldThe world.
scriptThe script entity.
instanceThe script instance.

◆ ecs_script_edits_apply()

FLECS_API char * ecs_script_edits_apply ( const ecs_script_edits_t * edits)

Apply an edit set.

This operation returns new script source code with the edits applied. The operation does not modify the script or the edit set, which means that applying the same edit set twice returns the same text.

The operation returns NULL when two edits overlap in a way that cannot be resolved.

Parameters
editsThe edit set.
Returns
The new source code, must be freed with ecs_os_free(). NULL if failed.

◆ ecs_script_edits_clear()

FLECS_API int ecs_script_edits_clear ( ecs_script_edits_t * edits,
ecs_entity_t entity,
ecs_id_t component )

Remove a recorded edit from an edit set.

Edits are keyed by entity and component. This operation removes the edit that was recorded for the provided key, which undoes the effect that the edit would have had on ecs_script_edits_apply().

Pass 0 for the component to clear the edit recorded by ecs_script_edits_delete(). Pass a component (or pair) to clear the edit recorded by ecs_script_edits_set(), ecs_script_edits_set_expr() or ecs_script_edits_remove() for that component.

The entity is only used as a key. It does not have to be alive, which means an edit that was recorded before the entity was deleted can still be cleared afterwards.

Parameters
editsThe edit set.
entityThe entity the edit was recorded for.
componentThe component the edit was recorded for, or 0 for a delete.
Returns
Zero if an edit was removed, non-zero if no edit was recorded.

◆ ecs_script_edits_count()

FLECS_API int32_t ecs_script_edits_count ( const ecs_script_edits_t * edits)

Return the number of recorded edits in an edit set.

Edits are keyed by entity and component, which means that recording an edit twice for the same key does not increase the count.

Parameters
editsThe edit set.
Returns
The number of recorded edits.

◆ ecs_script_edits_delete()

FLECS_API int ecs_script_edits_delete ( ecs_script_edits_t * edits,
ecs_entity_t entity )

Delete an entity.

This removes the entity statement, its scope, and the line(s) the statement occupies. Comments that precede the statement are preserved. A trailing comment on the last line of the statement is removed together with the line.

When the removed lines are surrounded by blank lines (where the start of the file, the opening brace of the enclosing scope, the closing brace of the enclosing scope and the end of the file count as blank), one of the blank lines is removed as well, so that the statements around the deleted statement stay separated by exactly one blank line.

Other edits that fall inside the span of a deleted entity (such as edits to child entities) are absorbed by the deletion.

When the entity was created by a template body statement (see ecs_script_entity_source()) the statement in the template body is deleted, which removes the entity from every instance of the template.

The operation only records the source code span of the statement, it does not delete the entity. The edit must be recorded while the entity is still alive; the recorded edit remains valid after the entity is deleted with ecs_delete(). See ecs_script_edits_new().

Parameters
editsThe edit set.
entityThe entity to delete.
Returns
Zero if success, non-zero if failed.

◆ ecs_script_edits_free()

FLECS_API void ecs_script_edits_free ( ecs_script_edits_t * edits)

Free an edit set.

Parameters
editsThe edit set.

◆ ecs_script_edits_new()

FLECS_API ecs_script_edits_t * ecs_script_edits_new ( ecs_script_t * script)

Create an edit set for a script.

An edit set collects changes that are keyed by entity id, and turns them into new script source code with ecs_script_edits_apply(). Only the edited statements change; all other text (comments, whitespace, layout, expressions, include statements) is preserved byte for byte.

The script must be evaluated before edits can be added, and must outlive the edit set.

Edits are resolved to a source code span when they are recorded, not when they are applied. The entity is only used to find the statement at that moment; afterwards the recorded edit no longer depends on it. This means an edit set can outlive the entities it was recorded for, but it also means that a delete must be recorded before the entity is deleted:

ecs_delete(world, e);
FLECS_API int ecs_script_edits_delete(ecs_script_edits_t *edits, ecs_entity_t entity)
Delete an entity.
void ecs_delete(ecs_world_t *world, ecs_entity_t entity)
Delete an entity.

Recording an edit for an entity that is no longer alive fails, as the statement can no longer be found.

A recorded edit can be dropped again with ecs_script_edits_clear(), which makes it possible to use a single edit set as an undo/redo buffer for an editing session.

An edit set must be deleted with ecs_script_edits_free().

Parameters
scriptThe script to edit.
Returns
A new edit set, or NULL if the script is invalid.

◆ ecs_script_edits_remove()

FLECS_API int ecs_script_edits_remove ( ecs_script_edits_t * edits,
ecs_entity_t entity,
ecs_id_t component )

Remove a component from an entity.

This removes the statement that adds the component (or tag) to the entity, including the line(s) the statement occupies. If the entity scope contains more than one statement for the component, the last statement is removed.

The operation returns zero when the entity does not have a statement for the component, as the resulting source has the requested state.

Parameters
editsThe edit set.
entityThe entity to edit.
componentThe component (or pair) to remove.
Returns
Zero if success, non-zero if failed.

◆ ecs_script_edits_set()

FLECS_API int ecs_script_edits_set ( ecs_script_edits_t * edits,
ecs_entity_t entity,
ecs_id_t component,
const void * value )

Set a component value on an entity.

If the entity scope already contains a statement for the component, only the value expression of that statement is replaced. The style of the existing initializer is preserved:

  • a named initializer ("{x: 10, y: 20}") stays named
  • a positional initializer ("{10, 20}") stays positional
  • an empty initializer ("{}") becomes positional
  • any other value form (a plain expression, a collection initializer, a match expression) is replaced with the default serialized form

All members of the component are written, in the order in which they are defined by the type. Floating point members are written as the shortest decimal string that parses back to the same value ("1.2345" for a float with value 1.2345f, "1" for a float with value 1.0f), so values roundtrip without accumulating digits.

When the entity was created by a template body statement (see ecs_script_entity_source()) the statement in the template body is edited, which changes the value for every instance of the template. Any expression that the body used for the value (such as a prop or const reference) is replaced by the literal value that is passed to this operation.

If the entity scope does not contain a statement for the component, a new "Component: {...}" statement is appended to the end of the entity scope, using the indentation of the other statements in the scope (or the indentation of the entity statement plus four spaces when the scope is empty). When the entity statement has no scope, a scope is added.

If the entity scope contains more than one statement for the component, the last statement is replaced. If the entity is declared by more than one statement, the first declaration is edited.

Only statements in the scope of the entity itself are considered. A value that the entity inherits from an enclosing with statement is not modified; setting such a component adds a statement to the entity scope that overrides the with value.

The component is written with the shortest name that resolves to the same component given the using and module statements of the script.

Parameters
editsThe edit set.
entityThe entity to edit.
componentThe component (or pair) to set.
valuePointer to the component value.
Returns
Zero if success, non-zero if failed.

◆ ecs_script_edits_set_expr()

FLECS_API int ecs_script_edits_set_expr ( ecs_script_edits_t * edits,
ecs_entity_t entity,
ecs_id_t component,
const char * expr )

Set a component value on an entity from an expression string.

Same as ecs_script_edits_set(), but instead of serializing a value, the provided expression is written to the script verbatim. The expression is not validated.

Parameters
editsThe edit set.
entityThe entity to edit.
componentThe component (or pair) to set.
exprThe value expression (for example "{10, 20}").
Returns
Zero if success, non-zero if failed.

◆ ecs_script_entity_owner()

FLECS_API ecs_entity_t ecs_script_entity_owner ( const ecs_world_t * world,
ecs_entity_t entity )

Find the managed script that declares an entity.

This operation returns the entity of the managed script (see ecs_script()) whose source code contains the statement that created the entity. The returned script entity is the one to use with ecs_script_entity_source() and ecs_script_edits_new():

const EcsScript *sc = ecs_get(world, s, EcsScript);
struct ecs_script_edits_t ecs_script_edits_t
Set of pending edits for a script.
Definition script.h:558
FLECS_API ecs_script_edits_t * ecs_script_edits_new(ecs_script_t *script)
Create an edit set for a script.
FLECS_API ecs_entity_t ecs_script_entity_owner(const ecs_world_t *world, ecs_entity_t entity)
Find the managed script that declares an entity.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:395
#define ecs_get(world, entity, T)
Get a component.
Definition flecs_c.h:339
Script component.
Definition script.h:122
ecs_script_t * script
Parsed script object.
Definition script.h:126

For entities that were created by a regular entity statement this is the script that created the entity. For entities that were created by a template body this is the script that declares the template, which is not necessarily the script that instantiates it.

The operation returns 0 when the entity is not editable, which is the case when the entity was not created by a script, when the script that created it is not managed (see ecs_script_parse()), or when the statement that created the entity cannot be attributed to the entity (see ecs_script_entity_source()).

Parameters
worldThe world.
entityThe entity to find.
Returns
The managed script entity, or 0 if the entity is not editable.

◆ ecs_script_entity_source()

FLECS_API bool ecs_script_entity_source ( const ecs_script_t * script,
ecs_entity_t entity,
ecs_script_source_t * source )

Find the statement that declares an entity.

This operation returns the source location of the entity statement that declared an entity. The script must have been evaluated at least once, as the mapping from entity to statement is established during evaluation.

Entities that were created by instantiating a template declared by this script are resolved to the statement in the template body that created them. Because a template body statement is shared by all instances of the template, the same statement is returned for every instance, and ecs_script_source_t::template_ is set to the template entity. The template must be declared by this script; when a script instantiates a template that is declared by another script (for example a template from an included file), the body entities are reported by the script that declares the template, not by the script that instantiates it.

The operation returns false when the entity was not created by an entity statement of this script. This is the case for:

  • entities that were not created by a script
  • entities created by a different script (an include statement evaluates the included file as a separate script object, so entities created by an included file are never reported by the including script)
  • entities created inside a for loop or a function body, also when the for loop is part of a template body
  • entities created by a template body statement with a computed name
  • entities created by a "new" expression

An entity that carries the (EcsScriptTemplate, *) pair is never reported as a plain (non-template) statement, also not by the script that instantiates the template.

When an entity is declared by more than one statement, the location of the first declaration is returned.

The returned offsets stay valid until the script is freed or reparsed.

Parameters
scriptThe script.
entityThe entity to find.
sourceOut parameter with the source location (optional).
Returns
True if the entity is declared by this script, false if not.

◆ ecs_script_eval()

FLECS_API int ecs_script_eval ( const ecs_script_t * script,
const ecs_script_eval_desc_t * desc,
ecs_script_eval_result_t * result )

Evaluate script.

This operation evaluates (runs) a parsed script.

When the result parameter is not NULL, the script will capture errors and return them in the output struct. If result.error is set, it must be freed by the application.

Parameters
scriptThe script.
descParameters for script runtime.
resultOutput of script evaluation (optional).
Returns
Zero if success, non-zero if failed.

◆ ecs_script_free()

FLECS_API void ecs_script_free ( ecs_script_t * script)

Free script.

This operation frees a script object.

Templates created by the script rely upon resources in the script object, and for that reason keep the script alive until all templates created by the script are deleted.

Parameters
scriptThe script.

◆ ecs_script_init()

FLECS_API ecs_entity_t ecs_script_init ( ecs_world_t * world,
const ecs_script_desc_t * desc )

Load managed script.

A managed script tracks which entities it creates, and keeps those entities synchronized when the contents of the script are updated. When the script is updated, entities that are no longer in the new version will be deleted.

This feature is experimental.

Parameters
worldThe world.
descScript descriptor.
Returns
The script entity.

◆ ecs_script_ir_to_str()

FLECS_API char * ecs_script_ir_to_str ( const ecs_script_t * script)

Convert script IR to string.

Compiles the script to IR if it hasn't been compiled yet, and returns a human readable listing of the instructions.

Parameters
scriptThe script.
Returns
The IR listing. Must be freed with ecs_os_free.

◆ ecs_script_parse()

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,
ecs_script_eval_result_t * result )

Parse script.

This operation parses a script and returns a script object upon success. To run the script, call ecs_script_eval().

When the result parameter is not NULL, the script will capture errors and return them in the output struct. If result.error is set, it must be freed by the application.

Parameters
worldThe world.
nameName of the script (typically a file or module name).
codeThe script code.
descParameters for script runtime.
resultOutput of script evaluation.
Returns
Script object if success, NULL if failed.

◆ ecs_script_run()

FLECS_API int ecs_script_run ( ecs_world_t * world,
const char * name,
const char * code,
ecs_script_eval_result_t * result )

Parse script.

This parses a script and instantiates the entities in the world. This operation is the equivalent to doing:

ecs_script_t *script = ecs_script_parse(world, name, code);
FLECS_API int ecs_script_eval(const ecs_script_t *script, const ecs_script_eval_desc_t *desc, ecs_script_eval_result_t *result)
Evaluate script.
FLECS_API void ecs_script_free(ecs_script_t *script)
Free script.
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, ecs_script_eval_result_t *result)
Parse script.
script_builder script(const char *name=nullptr) const
Build a script.
Definition mixin.inl:31
Script object.
Definition script.h:110
Parameters
worldThe world.
nameThe script name (typically the file).
codeThe script.
resultOutput of script evaluation (optional).
Returns
Zero if success, non-zero otherwise.

◆ ecs_script_run_file()

FLECS_API int ecs_script_run_file ( ecs_world_t * world,
const char * filename )

Parse script file.

This parses a script file and instantiates the entities in the world. This operation is equivalent to loading the file contents and passing it to ecs_script_run().

Parameters
worldThe world.
filenameThe script file name.
Returns
Zero if success, non-zero if failed.

◆ ecs_script_run_file_w_desc()

FLECS_API int ecs_script_run_file_w_desc ( ecs_world_t * world,
const char * filename,
const ecs_script_eval_desc_t * desc )

Parse script file and evaluate with options.

Same as ecs_script_run_file(), but accepts a descriptor that configures parsing and evaluation.

Parameters
worldThe world.
filenameThe script file name.
descParse and evaluation options (optional).
Returns
Zero if success, non-zero if failed.

◆ ecs_script_run_w_desc()

FLECS_API int ecs_script_run_w_desc ( ecs_world_t * world,
const char * name,
const char * code,
const ecs_script_eval_desc_t * desc,
ecs_script_eval_result_t * result )

Parse script and evaluate with options.

Same as ecs_script_run(), but accepts a descriptor that configures parsing and evaluation, such as skip_unknown or the runtime (AST or IR) to use.

Parameters
worldThe world.
nameThe script name (typically the file).
codeThe script.
descParse and evaluation options (optional).
resultOutput of script evaluation (optional).
Returns
Zero if success, non-zero if failed.

◆ ecs_script_runtime_free()

FLECS_API void ecs_script_runtime_free ( ecs_script_runtime_t * runtime)

Free script runtime.

This operation frees a script runtime created by ecs_script_runtime_new().

Parameters
runtimeThe runtime to free.

◆ ecs_script_runtime_new()

FLECS_API ecs_script_runtime_t * ecs_script_runtime_new ( void )

Create runtime for script.

A script runtime is a container for any data created during script evaluation. By default, calling ecs_script_run() or ecs_script_eval() will create a runtime on the spot. A runtime can be created in advance and reused across multiple script evaluations to improve performance.

When scripts are evaluated on multiple threads, each thread should have its own script runtime.

A script runtime must be deleted with ecs_script_runtime_free().

Returns
A new script runtime.

◆ 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.

This operation evaluates expressions in a string, and replaces them with their evaluated result. Supported expression formats are:

  • $variable_name
  • {expression}

The $, { and } characters can be escaped with a backslash ().

Parameters
worldThe world.
strThe string to evaluate.
varsThe variables to use for evaluation.
Returns
String with interpolated expressions, or NULL if failed.

◆ 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.

Parameters
worldThe world.
scriptThe script entity.
instanceA template instance (optional).
codeThe script code.
Returns
Zero if success, non-zero if failed.

◆ 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.

This operation declares a new variable in the current scope. If a variable with the specified name already exists, the operation will fail.

This operation does not allocate storage for the variable. This is done to allow for variables that point to existing storage, which prevents having to copy existing values to a variable scope.

Parameters
varsThe variable scope.
nameThe variable name.
Returns
The new variable, or NULL if the operation failed.

◆ 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.

This operation calls ecs_script_vars_declare() and allocates storage for the variable. If the type has a ctor, it will be called on the new storage.

The scope's stack allocator will be used to allocate the storage. After ecs_script_vars_pop() is called on the scope, the variable storage will no longer be valid.

The operation will fail if the type argument is not a type.

Parameters
varsThe variable scope.
nameThe variable name.
typeThe variable type.
Returns
The new variable, or NULL if the operation failed.

◆ ecs_script_vars_fini()

FLECS_API void ecs_script_vars_fini ( ecs_script_vars_t * vars)

Free variable scope.

Free root variable scope. The provided scope should not have a parent. This operation calls ecs_script_vars_pop() on the scope.

Parameters
varsThe variable scope.

◆ 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. This allows for using iterator results in expressions. The operation only converts a single result at a time, and does not progress the iterator.

Iterator fields with data will be made available as variables with as name the field index (e.g., "$1"). The operation does not check if reflection data is registered for a field type. If no reflection data is registered for the type, using the field variable in expressions will fail.

Field variables will only contain single elements, even if the iterator returns component arrays. The offset parameter can be used to specify which element in the component arrays to return. The offset parameter must be smaller than it->count.

The operation will create a variable for query variables that contain a single entity.

The operation will attempt to use existing variables. If a variable does not yet exist, the operation will create it. If an existing variable exists with a mismatching type, the operation will fail.

Accessing variables after progressing the iterator or after the iterator is destroyed will result in undefined behavior.

If vars contains a variable that is not present in the iterator, the variable will not be modified.

Parameters
itThe iterator to convert to variables.
varsThe variables to write to.
offsetThe offset to the current element.

◆ ecs_script_vars_from_sp()

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.

This operation provides a faster way to lookup variables that are always declared in the same order in a ecs_script_vars_t scope.

The stack pointer of a variable can be obtained from the ecs_script_var_t type. The provided frame offset must be valid for the provided variable
stack. If the frame offset is not valid, this operation will panic.

Parameters
varsThe variable scope.
spThe stack pointer to the variable.
Returns
The variable.

◆ ecs_script_vars_init()

FLECS_API ecs_script_vars_t * ecs_script_vars_init ( ecs_world_t * world)

Create new variable scope.

Create root variable scope. A variable scope contains one or more variables. Scopes can be nested, which allows variables in different scopes to have the same name. Variables from parent scopes will be shadowed by variables in child scopes with the same name.

Use the ecs_script_vars_push() and ecs_script_vars_pop() functions to push and pop variable scopes.

When a variable contains allocated resources (e.g., a string), its resources will be freed when ecs_script_vars_pop() is called on the scope, the ecs_script_vars_t::type_info field is initialized for the variable, and ecs_type_info_t::hooks::dtor is set.

Parameters
worldThe world.
Returns
The new root variable scope.

◆ 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.

This operation looks up a variable in the current scope. If the variable can't be found in the current scope, the operation will recursively search the parent scopes.

Parameters
varsThe variable scope.
nameThe variable name.
Returns
The variable, or NULL if one with the provided name does not exist.

◆ ecs_script_vars_pop()

FLECS_API ecs_script_vars_t * ecs_script_vars_pop ( ecs_script_vars_t * vars)

Pop variable scope.

This frees up the resources for a variable scope. The scope must be at the top of a vars stack. Calling ecs_script_vars_pop() on a scope that is not the last scope causes undefined behavior.

Parameters
varsThe scope to free.
Returns
The parent scope.

◆ ecs_script_vars_print()

FLECS_API void ecs_script_vars_print ( const ecs_script_vars_t * vars)

Print variables.

This operation prints all variables in the vars scope and parent scopes.

Parameters
varsThe 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.

Scopes created with ecs_script_vars_push() must be cleaned up with ecs_script_vars_pop().

If the stack and allocator arguments are left to NULL, their values will be copied from the parent.

Parameters
parentThe parent scope (provide NULL for root scope).
Returns
The new variable scope.

◆ ecs_script_vars_set_size()

FLECS_API void ecs_script_vars_set_size ( ecs_script_vars_t * vars,
int32_t count )

Preallocate space for variables.

This operation preallocates space for the specified number of variables. This is a performance optimization only, and is not necessary before declaring variables in a scope.

Parameters
varsThe variable scope.
countThe number of variables to preallocate space for.

◆ FlecsScriptImport()

FLECS_API void FlecsScriptImport ( ecs_world_t * world)

Script module import function.

Usage:

ECS_IMPORT(world, FlecsScript)
#define ECS_IMPORT(world, id)
Wrapper around ecs_import().
Definition module.h:125
Parameters
worldThe world.