Skip to content
Flecs v4.1
relationships/exclusive_relations

  • C

    View on GitHub

    1#include <exclusive_relations.h>
    2#include <stdio.h>
    3
    4int main(int argc, char *argv[]) {
    5 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    6
    7 ECS_TAG(ecs, Platoon);
    8
    9 // Register Platoon as exclusive relationship. This ensures that an entity
    10 // can only belong to a single Platoon.
    11 ecs_add_id(ecs, Platoon, EcsExclusive);
    12
    13 // Create two platoons
    14 ecs_entity_t platoon_1 = ecs_new(ecs);
    15 ecs_entity_t platoon_2 = ecs_new(ecs);
    16
    17 // Create a unit
    18 ecs_entity_t unit = ecs_new(ecs);
    19
    20 // Add unit to platoon 1
    21 ecs_add_pair(ecs, unit, Platoon, platoon_1);
    22
    23 // Log platoon of unit
    24 printf("Unit in platoon 1: %s\n",
    25 ecs_has_pair(ecs, unit, Platoon, platoon_1) ? "true" : "false");
    26 printf("Unit in platoon 2: %s\n\n",
    27 ecs_has_pair(ecs, unit, Platoon, platoon_2) ? "true" : "false");
    28
    29 // Add unit to platoon 2. Because Platoon is an exclusive relationship, this
    30 // both removes (Platoon, platoon_1) and adds (Platoon, platoon_2) in a
    31 // single operation.
    32 ecs_add_pair(ecs, unit, Platoon, platoon_2);
    33
    34 printf("Unit in platoon 1: %s\n",
    35 ecs_has_pair(ecs, unit, Platoon, platoon_1) ? "true" : "false");
    36 printf("Unit in platoon 2: %s\n",
    37 ecs_has_pair(ecs, unit, Platoon, platoon_2) ? "true" : "false");
    38
    39 // Output:
    40 // Unit in platoon 1: true
    41 // Unit in platoon 2: false
    42 //
    43 // Unit in platoon 1: false
    44 // Unit in platoon 2: true
    45
    46 return ecs_fini(ecs);
    47}
    void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
    Add a (component) ID to an entity.
    const ecs_entity_t EcsExclusive
    Can be added to a relationship to indicate that the relationship can only occur once on an entity.
    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
    ecs_entity_t ecs_new(ecs_world_t *world)
    Create new entity ID.
    #define ecs_add_pair(world, subject, first, second)
    Add a pair to an entity.
    Definition flecs_c.h:275
    #define ECS_TAG(world, id)
    Declare and define a tag.
    Definition flecs_c.h:59
    #define ecs_has_pair(world, entity, first, second)
    Test if an entity has a pair.
    Definition flecs_c.h:527
    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 <exclusive_relations.h>
    2#include <iostream>
    3
    4// Type for Platoon relationship
    5struct Platoon { };
    6
    7int main(int, char*[]) {
    8 flecs::world ecs;
    9
    10 // Register Platoon as exclusive relationship. This ensures that an entity
    11 // can only belong to a single Platoon.
    12 ecs.component<Platoon>()
    13 .add(flecs::Exclusive);
    14
    15 // Create two platoons
    16 flecs::entity platoon_1 = ecs.entity();
    17 flecs::entity platoon_2 = ecs.entity();
    18
    19 // Create a unit
    20 flecs::entity unit = ecs.entity();
    21
    22 // Add unit to platoon 1
    23 unit.add<Platoon>(platoon_1);
    24
    25 // Log platoon of unit
    26 std::cout << "Unit in platoon 1: " <<
    27 (unit.has<Platoon>(platoon_1) ? "true" : "false") << "\n";
    28 std::cout << "Unit in platoon 2: " <<
    29 (unit.has<Platoon>(platoon_2) ? "true" : "false") << "\n\n";
    30
    31 // Add unit to platoon 2. Because Platoon is an exclusive relationship, this
    32 // both removes (Platoon, platoon_1) and adds (Platoon, platoon_2) in a
    33 // single operation.
    34 unit.add<Platoon>(platoon_2);
    35
    36 std::cout << "Unit in platoon 1: " <<
    37 (unit.has<Platoon>(platoon_1) ? "true" : "false") << "\n";
    38 std::cout << "Unit in platoon 2: " <<
    39 (unit.has<Platoon>(platoon_2) ? "true" : "false") << "\n";
    40
    41 // Output:
    42 // Unit in platoon 1: true
    43 // Unit in platoon 2: false
    44 //
    45 // Unit in platoon 1: false
    46 // Unit in platoon 2: true
    47}
    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
    Entity.
    Definition entity.hpp:30
    The world.
    Definition world.hpp:129