Skip to content
Flecs v4.1
relationships/enum_relations

  • C++

    View on GitHub

    1#include <enum_relations.h>
    2#include <stdio.h>
    3
    4// When an enumeration constant is added to an entity, it is added as a relationship
    5// pair where the relationship is the enum type, and the target is the constant. For
    6// example, this statement:
    7// e.add(Color::Red)
    8//
    9// adds this relationship:
    10// (Color, Color::Red)
    11//
    12// Enums are registered as exclusive relationships, which means that adding an
    13// enum constant will replace the previous constant for that enumeration:
    14// e.add(Color::Green)
    15//
    16// will replace Color::Red with Color::Green
    17
    18// Regular enumerations are supported
    19enum Tile {
    20 Grass,
    21 Sand,
    22 Stone
    23};
    24
    25// Enum classes are supported
    26enum class TileStatus {
    27 Free,
    28 Occupied,
    29};
    30
    31int main(int, char *[]) {
    32 flecs::world ecs;
    33
    34 // Create an entity with (Tile, Stone) and (TileStatus, Free) relationships
    35 flecs::entity tile = ecs.entity()
    36 .add(Tile::Stone)
    37 .add(TileStatus::Free);
    38
    39 // (Tile, Tile.Stone), (TileStatus, TileStatus.Free)
    40 printf("%s\n", tile.type().str().c_str());
    41
    42 // Replace (TileStatus, Free) with (TileStatus, Occupied)
    43 tile.add(TileStatus::Occupied);
    44
    45 // (Tile, Tile.Stone), (TileStatus, TileStatus.Occupied)
    46 printf("%s\n", tile.type().str().c_str());
    47
    48 // Check if the entity has the Tile relationship and the Tile::Stone pair
    49 printf("%s\n", tile.has<Tile>() ? "true" : "false"); // true
    50 printf("%s\n", tile.has(Tile::Stone) ? "true" : "false"); // true
    51
    52 // Get the current value of the enum
    53 Tile v = tile.get_constant<Tile>();
    54 printf("%s\n", (v == Tile::Stone) ? "true" : "false"); // true
    55
    56 // Create a few more entities that we can query
    57 ecs.entity().add(Tile::Grass).add(TileStatus::Free);
    58 ecs.entity().add(Tile::Sand).add(TileStatus::Occupied);
    59
    60 // Iterate all entities with a Tile relationship
    61 ecs.query_builder()
    62 .with<Tile>(flecs::Wildcard)
    63 .build()
    64 .each([&](flecs::iter& it, size_t) {
    65 flecs::entity tile_constant = it.pair(0).second();
    66 printf("%s\n", tile_constant.path().c_str());
    67 });
    68
    69 // Outputs:
    70 // ::Tile::Stone
    71 // ::Tile::Grass
    72 // ::Tile::Sand
    73
    74 // Iterate only occupied tiles
    75 ecs.query_builder()
    76 .with<Tile>(flecs::Wildcard)
    77 .with(TileStatus::Occupied)
    78 .build()
    79 .each([&](flecs::iter& it, size_t) {
    80 flecs::entity tile_constant = it.pair(0).second();
    81 printf("%s\n", tile_constant.path().c_str());
    82 });
    83
    84 // Outputs:
    85 // ::Tile::Stone
    86 // ::Tile::Sand
    87
    88 // Remove any instance of the TileStatus relationship
    89 tile.remove<TileStatus>(flecs::Wildcard);
    90}
    flecs::query_builder< Comps... > query_builder(Args &&... args) const
    Create a query builder.
    Definition impl.hpp:342
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::type type() const
    Get the entity's type.
    Definition impl.hpp:94
    flecs::string path(const char *sep="::", const char *init_sep="::") const
    Return the entity path.
    Enum get_constant() const
    Get enum constant for enum relationship.
    Definition impl.hpp:30
    Entity.
    Definition entity.hpp:30
    flecs::entity second() const
    Get second element from a pair.
    Definition impl.hpp:31
    Class for iterating over query results.
    Definition iter.hpp:68
    flecs::id pair(int8_t index) const
    Obtain the pair ID matched for the field.
    Definition iter.hpp:53
    const char * c_str() const
    Return the C string.
    Definition string.hpp:109
    flecs::string str() const
    Convert the type to a comma-separated string.
    Definition type.hpp:38
    The world.
    Definition world.hpp:129