Skip to content
Flecs v4.1
script/functions

  • C

    View on GitHub

    1#include <functions.h>
    2#include <stdio.h>
    3
    4/* This example shows how to use user-defined functions in scripts. */
    5
    6const char *code =
    7 "e {\n"
    8 " Position: { sum(10, 20), (30).add(40) }\n"
    9 "}\n"
    10 ;
    11
    12typedef struct {
    13 int32_t x, y;
    14} Position;
    15
    16void sum(
    17 const ecs_function_ctx_t *ctx,
    18 int argc,
    19 const ecs_value_t *argv,
    20 ecs_value_t *result)
    21{
    22 (void)ctx;
    23 (void)argc;
    24
    25 *(int64_t*)result->ptr =
    26 *(int64_t*)argv[0].ptr +
    27 *(int64_t*)argv[1].ptr;
    28}
    29
    30int main(int argc, char *argv[]) {
    31 ecs_world_t *world = ecs_init_w_args(argc, argv);
    32
    33 ECS_COMPONENT(world, Position);
    34
    35 // Add reflection data to component so we can assign it in script
    36 ecs_struct(world, {
    37 .entity = ecs_id(Position), // Make sure to use existing id
    38 .members = {
    39 { .name = "x", .type = ecs_id(ecs_i32_t) },
    40 { .name = "y", .type = ecs_id(ecs_i32_t) }
    41 }
    42 });
    43
    44 // Define a sum function
    45 ecs_function(world, {
    46 .name = "sum",
    47 .return_type = ecs_id(ecs_i64_t),
    48 .params = {
    49 { .name = "a", .type = ecs_id(ecs_i64_t) },
    50 { .name = "b", .type = ecs_id(ecs_i64_t) }
    51 },
    52 .callback = sum
    53 });
    54
    55 // Define an add method. A method is like a function, except that it can be
    56 // called on type instances, and receives the instance as first parameter.
    57 ecs_method(world, {
    58 .name = "add",
    59 .parent = ecs_id(ecs_i64_t), // Add method to i64
    60 .return_type = ecs_id(ecs_i64_t),
    61 .params = {
    62 { .name = "a", .type = ecs_id(ecs_i64_t) }
    63 },
    64 .callback = sum
    65 });
    66
    67 if (ecs_script_run(world, "My script", code, NULL)) {
    68 printf("script failed to run\n");
    69 return -1;
    70 }
    71
    72 // Get entity and Position component
    73 ecs_entity_t e = ecs_lookup(world, "e");
    74 const Position *p = ecs_get(world, e, Position);
    75
    76 printf("{x: %d, y: %d}\n", p->x, p->y);
    77
    78 return ecs_fini(world);
    79
    80 // Output
    81 // {30, 70}
    82}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    int64_t ecs_i64_t
    Built-in i64 type.
    Definition meta.h:106
    #define ecs_struct(world,...)
    Create a struct type.
    Definition meta.h:1777
    int32_t ecs_i32_t
    Built-in i32 type.
    Definition meta.h:105
    FLECS_API int ecs_script_run(ecs_world_t *world, const char *name, const char *code, ecs_script_eval_result_t *result)
    Parse script.
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:395
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_get(world, entity, T)
    Get a component.
    Definition flecs_c.h:339
    ecs_entity_t ecs_lookup(const ecs_world_t *world, const char *path)
    Look up an entity by its path.
    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.
    Script function context.
    Definition script.h:143
    Value of a dynamic type.
    Definition flecs.h:1068
    void * ptr
    Pointer to value.
    Definition flecs.h:1070