Skip to content
Flecs v4.1
script/expr_w_vars

  • C

    View on GitHub

    1#include <expr_w_vars.h>
    2#include <stdio.h>
    3
    4// This example shows how to run an expression that uses variables. Variables
    5// make it possible to change the inputs to an expression after the expression
    6// has been parsed. This example also shows how to parse an expression and
    7// evaluate it multiple times.
    8
    9int main(int argc, char *argv[]) {
    10 ecs_world_t *world = ecs_init_w_args(argc, argv);
    11
    12 // Declare variables that we'll use in the expression
    14 ecs_script_var_t *x = ecs_script_vars_define(vars, "x", ecs_i32_t);
    15 ecs_script_var_t *y = ecs_script_vars_define(vars, "y", ecs_i32_t);
    16
    17 // Assign values to variables
    18 *(int32_t*)x->value.ptr = 10;
    19 *(int32_t*)y->value.ptr = 20;
    20
    21 // Declare value that will hold the expression result. Specify that we want
    22 // the returned value to be of type i32. If the expression type doesn't
    23 // match the specified type, it will be casted.
    24 int32_t result = 0;
    25 ecs_value_t result_value = {
    26 .type = ecs_id(ecs_i32_t),
    27 .ptr = &result
    28 };
    29
    30 // Parse expression
    31 ecs_expr_eval_desc_t desc = { .vars = vars };
    32 ecs_script_t *s = ecs_expr_parse(world, "$x + $y", &desc);
    33 if (!s) {
    34 printf("failed to parse expression\n");
    35 return -1;
    36 }
    37
    38 // Evaluate expression with variables
    39 if (ecs_expr_eval(s, &result_value, &desc)) {
    40 printf("failed to evaluate expression\n");
    41 return -1;
    42 }
    43
    44 printf("result = %d\n", result);
    45
    46 // Change variable and reevaluate expression
    47 *(int32_t*)y->value.ptr = 30;
    48 if (ecs_expr_eval(s, &result_value, &desc)) {
    49 printf("failed to evaluate expression\n");
    50 return -1;
    51 }
    52
    53 printf("result = %d\n", result);
    54
    55 // Free resources
    58
    59 return ecs_fini(world);
    60
    61 // Output
    62 // result = 30
    63 // result = 40
    64}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    int32_t ecs_i32_t
    Built-in i32 type.
    Definition meta.h:105
    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.
    FLECS_API void ecs_script_free(ecs_script_t *script)
    Free script.
    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_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.
    Used with ecs_expr_run().
    Definition script.h:1106
    Script object.
    Definition script.h:118
    Script variable.
    Definition script.h:94
    ecs_value_t value
    Variable value.
    Definition script.h:96
    Script variable scope.
    Definition script.h:104
    Value of a dynamic type.
    Definition flecs.h:1068
    void * ptr
    Pointer to value.
    Definition flecs.h:1070