Skip to content
Flecs v4.1
reflection/member_ranges

  • C

    View on GitHub

    1#include <member_ranges.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double value;
    6} CpuUtilization;
    7
    8int main(int argc, char *argv[]) {
    9 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    10
    11 ECS_COMPONENT(ecs, CpuUtilization);
    12
    13 // Register reflection data warning and error ranges. This adds metadata to
    14 // a type which can be used by tools (like the explorer) to visualize data.
    15 ecs_struct(ecs, {
    16 .entity = ecs_id(CpuUtilization),
    17 .members = {{
    18 .name = "value",
    19 .type = ecs_id(ecs_f64_t),
    20 .range = {0, 100}, // Specifics values that the member can assume
    21 .warning_range = {0, 60}, // Values outside this range are considered a warning
    22 .error_range = {0, 80} // Values outside this range are considered an error
    23 }}
    24 });
    25
    26 ecs_entity_t m1 = ecs_entity(ecs, { .name = "MachineA" });
    27 ecs_set(ecs, m1, CpuUtilization, {50});
    28
    29 ecs_entity_t m2 = ecs_entity(ecs, { .name = "MachineB" });
    30 ecs_set(ecs, m2, CpuUtilization, {75});
    31
    32 ecs_entity_t m3 = ecs_entity(ecs, { .name = "MachineC" });
    33 ecs_set(ecs, m3, CpuUtilization, {90});
    34
    35 // Uncomment this line and open
    36 // https://www.flecs.dev/explorer?show=query&query=CpuUtilization
    37 // to see how ranges affect visualization:
    38 // return ecs_app_run(ecs, &(ecs_app_desc_t){ .enable_rest = true });
    39 return ecs_fini(ecs);
    40}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    double ecs_f64_t
    Built-in f64 type.
    Definition meta.h:109
    #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_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    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.

  • C++

    View on GitHub

    1#include <member_ranges.h>
    2#include <iostream>
    3
    4struct CpuUtilization {
    5 double value;
    6};
    7
    8int main() {
    9 flecs::world ecs;
    10
    11 ecs.component<CpuUtilization>()
    12 .member<double>("value")
    13 .range(0.0, 100.0) // Specifics values that the member can assume
    14 .warning_range(0.0, 60.0) // Values outside this range are considered a warning
    15 .error_range(0.0, 80.0); // Values outside this range are considered an error
    16
    17 ecs.entity("MachineA").set<CpuUtilization>({ 50.0 });
    18 ecs.entity("MachineB").set<CpuUtilization>({ 75.0 });
    19 ecs.entity("MachineC").set<CpuUtilization>({ 90.0 });
    20
    21 // Uncomment this line and open
    22 // https://www.flecs.dev/explorer?show=query&query=CpuUtilization
    23 // to see how ranges affect visualization:
    24 // ecs.app().enable_rest().run();
    25}
    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
    The world.
    Definition world.hpp:129