Skip to content
Flecs v4.1
reflection/world_ser_deser

  • C

    View on GitHub

    1#include <world_ser_deser.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x, y;
    6} Position, Velocity;
    7
    10
    11void Move(ecs_iter_t *it) {
    12 Position *p = ecs_field(it, Position, 0);
    13 const Velocity *v = ecs_field(it, Velocity, 1);
    14
    15 for (int i = 0; i < it->count; i ++) {
    16 p[i].x += v[i].x;
    17 p[i].y += v[i].y;
    18 printf("%s: {%f, %f}\n", ecs_get_name(it->stage, it->entities[i]),
    19 p[i].x, p[i].y);
    20 }
    21}
    22
    23// Register components and systems in a module. This excludes them by default
    24// from the serialized data, and makes it easier to import across worlds.
    25void MoveModuleImport(ecs_world_t *world) {
    26 ECS_MODULE(world, MoveModule);
    27
    28 ECS_COMPONENT_DEFINE(world, Position);
    29 ECS_COMPONENT_DEFINE(world, Velocity);
    30
    31 ecs_struct(world, {
    32 .entity = ecs_id(Position),
    33 .members = {
    34 { .name = "x", .type = ecs_id(ecs_f64_t) },
    35 { .name = "y", .type = ecs_id(ecs_f64_t) },
    36 }
    37 });
    38
    39 ecs_struct(world, {
    40 .entity = ecs_id(Velocity),
    41 .members = {
    42 { .name = "x", .type = ecs_id(ecs_f64_t) },
    43 { .name = "y", .type = ecs_id(ecs_f64_t) },
    44 }
    45 });
    46
    47 ECS_SYSTEM(world, Move, EcsOnUpdate, Position, [in] Velocity);
    48}
    49
    50int main(int argc, char *argv[]) {
    51 ecs_world_t *world_a = ecs_init_w_args(argc, argv); {
    52 ECS_IMPORT(world_a, MoveModule); // put in a scope so variable doesn't overlap
    53 }
    54
    55 ecs_entity_t ent_1 = ecs_entity(world_a, { .name = "ent_1" });
    56 ecs_entity_t ent_2 = ecs_entity(world_a, { .name = "ent_2" });
    57 ecs_set(world_a, ent_1, Position, {10, 20});
    58 ecs_set(world_a, ent_2, Position, {30, 40});
    59 ecs_set(world_a, ent_1, Velocity, {1, -1});
    60 ecs_set(world_a, ent_2, Velocity, {-1, 1});
    61
    62 // Serialize world to JSON
    63 char *json = ecs_world_to_json(world_a, NULL);
    64 printf("%s\n\n", json);
    65
    66 // Output:
    67 // {
    68 // "results": [{
    69 // "ids": [
    70 // ["my_module.Position"],
    71 // ["my_module.Velocity"],
    72 // ["flecs.core.Identifier", "flecs.core.Name"]
    73 // ],
    74 // "entities": ["ent_1", "ent_2"],
    75 // "values": [
    76 // [{
    77 // "x": 10,
    78 // "y": 20
    79 // }, {
    80 // "x": 30,
    81 // "y": 40
    82 // }],
    83 // [{
    84 // "x": 1,
    85 // "y": -1
    86 // }, {
    87 // "x": -1,
    88 // "y": 1
    89 // }], 0
    90 // ]
    91 // }]
    92 // }
    93
    94 // Create second world, import same module
    95 ecs_world_t *world_b = ecs_init(); {
    96 ECS_IMPORT(world_b, MoveModule);
    97 }
    98
    99 // Deserialize JSON into second world
    100 ecs_world_from_json(world_b, json, NULL);
    101
    102 // Run system once for both worlds
    103 ecs_progress(world_a, 0);
    104 printf("\n");
    105 ecs_progress(world_b, 0);
    106
    107 // Output
    108 // ent_1: {11.000000, 19.000000}
    109 // ent_2: {29.000000, 41.000000}
    110 //
    111 // ent_1: {11.000000, 19.000000}
    112 // ent_2: {29.000000, 41.000000}
    113
    114 ecs_fini(world_a);
    115 ecs_fini(world_b);
    116
    117 ecs_os_free(json);
    118
    119 return 0;
    120}
    const ecs_entity_t EcsOnUpdate
    OnUpdate pipeline phase.
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    FLECS_API const char * ecs_world_from_json(ecs_world_t *world, const char *json, const ecs_from_json_desc_t *desc)
    Parse JSON object with multiple entities into the world.
    FLECS_API char * ecs_world_to_json(ecs_world_t *world, const ecs_world_to_json_desc_t *desc)
    Serialize world into JSON string.
    double ecs_f64_t
    Built-in f64 type.
    Definition meta.h:109
    #define ecs_struct(world,...)
    Create a struct type.
    Definition meta.h:1777
    #define ECS_MODULE(world, id)
    Create a module.
    Definition module.h:113
    #define ECS_IMPORT(world, id)
    Wrapper around ecs_import().
    Definition module.h:125
    FLECS_API bool ecs_progress(ecs_world_t *world, ecs_ftime_t delta_time)
    Progress a world.
    #define ECS_SYSTEM(world, id, phase,...)
    Declare and define a system.
    Definition system.h:263
    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_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ECS_COMPONENT_DEFINE(world, id_)
    Define a forward-declared component.
    Definition flecs_c.h:75
    #define ECS_COMPONENT_DECLARE(id)
    Forward declare a component.
    Definition flecs_c.h:65
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    #define ecs_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
    Get the name of an entity.
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init(void)
    Create a new world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.
    Iterator.
    Definition flecs.h:1183
    int32_t count
    Number of entities to iterate.
    Definition flecs.h:1189
    const ecs_entity_t * entities
    Entity identifiers.
    Definition flecs.h:1190

  • C++

    View on GitHub

    1#include <world_ser_deser.h>
    2#include <iostream>
    3#include <string>
    4
    5struct Position {
    6 double x;
    7 double y;
    8};
    9
    10struct Velocity {
    11 double x;
    12 double y;
    13};
    14
    15// Register components and systems in a module. This excludes them by default
    16// from the serialized data, and makes it easier to import across worlds.
    17struct move {
    18 move(flecs::world& world) {
    19 world.component<Position>()
    20 .member<double>("x")
    21 .member<double>("y");
    22
    23 world.component<Velocity>()
    24 .member<double>("x")
    25 .member<double>("y");
    26
    27 world.system<Position, const Velocity>("Move")
    28 .each([](flecs::entity e, Position& p, const Velocity& v) {
    29 p.x += v.x;
    30 p.y += v.y;
    31 std::cout << e.path() << " moved to "
    32 << "{x: " << p.x << ", y: " << p.y << "}"
    33 << std::endl;
    34 });
    35 }
    36};
    37
    38int main(int, char *[]) {
    39 flecs::world world_a;
    40
    41 world_a.import<move>();
    42
    43 world_a.entity("ent_1")
    44 .set<Position>({10, 20})
    45 .set<Velocity>({1, -1});
    46
    47 world_a.entity("ent_2")
    48 .set<Position>({30, 40})
    49 .set<Velocity>({-1, 1});
    50
    51 // Serialize world to JSON
    52 auto json = world_a.to_json();
    53 std::cout << json << std::endl << std::endl;
    54
    55 // Output:
    56 // {
    57 // "results": [{
    58 // "ids": [
    59 // ["my_module.Position"],
    60 // ["my_module.Velocity"],
    61 // ["flecs.core.Identifier", "flecs.core.Name"]
    62 // ],
    63 // "entities": ["ent_1", "ent_2"],
    64 // "values": [
    65 // [{
    66 // "x": 10,
    67 // "y": 20
    68 // }, {
    69 // "x": 30,
    70 // "y": 40
    71 // }],
    72 // [{
    73 // "x": 1,
    74 // "y": -1
    75 // }, {
    76 // "x": -1,
    77 // "y": 1
    78 // }], 0
    79 // ]
    80 // }]
    81 // }
    82
    83 // Create second world, import same module
    84 flecs::world world_b;
    85 world_b.import<move>();
    86
    87 // Deserialize JSON into second world
    88 world_b.from_json(json);
    89
    90 // Run system once for both worlds
    91 world_a.progress();
    92 std::cout << std::endl;
    93 world_b.progress();
    94
    95 // Output
    96 // ::ent_1 moved to {x: 11, y: 19}
    97 // ::ent_2 moved to {x: 29, y: 41}
    98 //
    99 // ::ent_1 moved to {x: 11, y: 19}
    100 // ::ent_2 moved to {x: 29, y: 41}
    101}
    const char * from_json(flecs::entity_t tid, void *value, const char *json, flecs::from_json_desc_t *desc=nullptr)
    Deserialize a value from JSON.
    Definition world.hpp:42
    flecs::string to_json(flecs::entity_t tid, const void *value) const
    Serialize an untyped value to JSON.
    Definition world.hpp:12
    flecs::entity import()
    Import a module.
    Definition impl.hpp:150
    flecs::system system(flecs::entity e) const
    Upcast an entity to a system.
    Definition impl.hpp:181
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    bool progress(ecs_ftime_t delta_time=0.0) const
    Progress the world one tick.
    Definition impl.hpp:47
    flecs::string path(const char *sep="::", const char *init_sep="::") const
    Return the entity path.
    The world.
    Definition world.hpp:129
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.