Skip to content
Flecs v4.1
reflection/query_to_json

  • C

    View on GitHub

    1#include <query_to_json.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 float x, y;
    6} Position, Velocity;
    7
    8typedef struct {
    9 float value;
    10} Mass;
    11
    12int main(int argc, char *argv[]) {
    13 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    14
    15 // Register component as usual
    16 ECS_COMPONENT(ecs, Position);
    17 ECS_COMPONENT(ecs, Velocity);
    18 ECS_COMPONENT(ecs, Mass);
    19
    20 // Add reflection data to components
    21 ecs_struct(ecs, {
    22 .entity = ecs_id(Position), // Make sure to use existing id
    23 .members = {
    24 { .name = "x", .type = ecs_id(ecs_f32_t) },
    25 { .name = "y", .type = ecs_id(ecs_f32_t) }
    26 }
    27 });
    28
    29 ecs_struct(ecs, {
    30 .entity = ecs_id(Velocity), // Make sure to use existing id
    31 .members = {
    32 { .name = "x", .type = ecs_id(ecs_f32_t) },
    33 { .name = "y", .type = ecs_id(ecs_f32_t) }
    34 }
    35 });
    36
    37 ecs_struct(ecs, {
    38 .entity = ecs_id(Mass), // Make sure to use existing id
    39 .members = {
    40 { .name = "value", .type = ecs_id(ecs_f32_t) }
    41 }
    42 });
    43
    44 ecs_entity_t a = ecs_set_name(ecs, 0, "a");
    45 ecs_entity_t b = ecs_set_name(ecs, 0, "b");
    46 ecs_entity_t c = ecs_set_name(ecs, 0, "c");
    47 ecs_entity_t d = ecs_set_name(ecs, 0, "d");
    48
    49 ecs_set(ecs, a, Position, {10, 20});
    50 ecs_set(ecs, b, Position, {20, 30});
    51 ecs_set(ecs, c, Position, {30, 40});
    52 ecs_set(ecs, d, Position, {40, 50});
    53
    54 ecs_set(ecs, a, Velocity, {1, 2});
    55 ecs_set(ecs, b, Velocity, {2, 3});
    56 ecs_set(ecs, c, Velocity, {3, 4});
    57 ecs_set(ecs, d, Velocity, {4, 5});
    58
    59 ecs_set(ecs, c, Mass, {10});
    60 ecs_set(ecs, d, Mass, {20});
    61
    62 // Query for components
    63 ecs_query_t *q = ecs_query(ecs, {
    64 .terms = {
    65 { .id = ecs_id(Position) }, { .id = ecs_id(Velocity) }
    66 }
    67 });
    68
    69 // Serialize query to JSON. This works for any iterator.
    70 ecs_iter_t it = ecs_query_iter(ecs, q);
    71 char *json = ecs_iter_to_json(&it, NULL);
    72 printf("%s\n", json);
    73 ecs_os_free(json);
    74
    75 // Iterator returns 2 sets of results, one for each table.
    76
    77 // {
    78 // "ids": ["Position", "Velocity"],
    79 // "results": [{
    80 // "ids": ["Position", "Velocity"],
    81 // "sources": [0, 0],
    82 // "is_set": [true, true],
    83 // "entities": ["a", "b"],
    84 // "values": [
    85 // [{
    86 // "x": 10.00,
    87 // "y": 20.00
    88 // }, {
    89 // "x": 20.00,
    90 // "y": 30.00
    91 // }],
    92 // [{
    93 // "x": 1.00,
    94 // "y": 2.00
    95 // }, {
    96 // "x": 2.00,
    97 // "y": 3.00
    98 // }]
    99 // ]
    100 // }, {
    101 // "ids": ["Position", "Velocity"],
    102 // "sources": [0, 0],
    103 // "is_set": [true, true],
    104 // "entities": ["c", "d"],
    105 // "values": [
    106 // [{
    107 // "x": 30.00,
    108 // "y": 40.00
    109 // }, {
    110 // "x": 40.00,
    111 // "y": 50.00
    112 // }],
    113 // [{
    114 // "x": 3.00,
    115 // "y": 4.00
    116 // }, {
    117 // "x": 4.00,
    118 // "y": 5.00
    119 // }]
    120 // ]
    121 // }]
    122 // }
    123
    124 ecs_fini(ecs);
    125}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    FLECS_API char * ecs_iter_to_json(ecs_iter_t *iter, const ecs_iter_to_json_desc_t *desc)
    Serialize iterator into JSON string.
    float ecs_f32_t
    Built-in f32 type.
    Definition meta.h:108
    #define ecs_struct(world,...)
    Create a struct type.
    Definition meta.h:1777
    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_query(world,...)
    Shorthand for creating a query with ecs_query_init().
    Definition flecs_c.h:215
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    ecs_entity_t ecs_set_name(ecs_world_t *world, ecs_entity_t entity, const char *name)
    Set the name of an entity.
    ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
    Create a query iterator.
    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.
    Iterator.
    Definition flecs.h:1183
    Queries are lists of constraints (terms) that match entities.
    Definition flecs.h:858

  • C++

    View on GitHub

    1#include <query_to_json.h>
    2#include <iostream>
    3
    4struct Position {
    5 float x;
    6 float y;
    7};
    8
    9struct Velocity {
    10 float x;
    11 float y;
    12};
    13
    14struct Mass {
    15 float value;
    16};
    17
    18int main(int, char *[]) {
    19 flecs::world ecs;
    20
    21 // Register components with reflection data
    22 ecs.component<Position>()
    23 .member<float>("x")
    24 .member<float>("y");
    25
    26 ecs.component<Velocity>()
    27 .member<float>("x")
    28 .member<float>("y");
    29
    30 ecs.component<Mass>()
    31 .member<float>("value");
    32
    33 ecs.entity("a").set<Position>({10, 20}).set<Velocity>({1, 2});
    34 ecs.entity("b").set<Position>({20, 30}).set<Velocity>({2, 3});
    35 ecs.entity("c").set<Position>({30, 40}).set<Velocity>({3, 4}).set<Mass>({10});
    36 ecs.entity("d").set<Position>({30, 40}).set<Velocity>({4, 5}).set<Mass>({20});
    37
    38 // Query for components
    40 ecs.query<Position, const Velocity>();
    41
    42 // Serialize query to JSON. Note that this works for any iterable object.
    43 std::cout << q.iter().to_json().c_str() << "\n";
    44
    45 // Iterator returns 2 sets of results, one for each table.
    46
    47 // {
    48 // "ids": ["Position", "Velocity"],
    49 // "results": [{
    50 // "ids": ["Position", "Velocity"],
    51 // "sources": [0, 0],
    52 // "is_set": [true, true],
    53 // "entities": ["a", "b"],
    54 // "values": [
    55 // [{
    56 // "x": 10.00,
    57 // "y": 20.00
    58 // }, {
    59 // "x": 20.00,
    60 // "y": 30.00
    61 // }],
    62 // [{
    63 // "x": 1.00,
    64 // "y": 2.00
    65 // }, {
    66 // "x": 2.00,
    67 // "y": 3.00
    68 // }]
    69 // ]
    70 // }, {
    71 // "ids": ["Position", "Velocity"],
    72 // "sources": [0, 0],
    73 // "is_set": [true, true],
    74 // "entities": ["c", "d"],
    75 // "values": [
    76 // [{
    77 // "x": 30.00,
    78 // "y": 40.00
    79 // }, {
    80 // "x": 30.00,
    81 // "y": 40.00
    82 // }],
    83 // [{
    84 // "x": 3.00,
    85 // "y": 4.00
    86 // }, {
    87 // "x": 4.00,
    88 // "y": 5.00
    89 // }]
    90 // ]
    91 // }]
    92 // }
    93}
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    conditional_t< sizeof...(Args)==0 &&_::is_sparse_query< Comps... >::value, flecs::sparse_query< Comps... >, flecs::query< Comps... > > query(Args &&... args) const
    Create a query.
    Definition impl.hpp:326
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129