Skip to content
Flecs v4.1
relationships/symmetric_relations

  • C

    View on GitHub

    1#include <symmetric_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, TradesWith);
    8
    9 // Register TradesWith as symmetric relationship. Symmetric relationships
    10 // go both ways, adding (R, B) to A will also add (R, A) to B.
    11 ecs_add_id(ecs, TradesWith, EcsSymmetric);
    12
    13 // Create two players
    14 ecs_entity_t player_1 = ecs_new(ecs);
    15 ecs_entity_t player_2 = ecs_new(ecs);
    16
    17 // Add (TradesWith, player_2) to player_1. This also adds
    18 // (TradesWith, player_1) to player_2.
    19 ecs_add_pair(ecs, player_1, TradesWith, player_2);
    20
    21 // Log platoon of unit
    22 printf("Player 1 trades with Player 2: %s\n",
    23 (ecs_has_pair(ecs, player_1, TradesWith, player_2) ? "true" : "false"));
    24 printf("Player 2 trades with Player 1: %s\n",
    25 (ecs_has_pair(ecs, player_2, TradesWith, player_1) ? "true" : "false"));
    26
    27 // Output:
    28 // Player 1 trades with Player 2: true
    29 // Player 2 trades with Player 1: true
    30
    31 return ecs_fini(ecs);
    32}
    void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
    Add a (component) ID to an entity.
    FLECS_API const ecs_entity_t EcsSymmetric
    Mark relationship as commutative.
    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 <symmetric_relations.h>
    2#include <iostream>
    3
    4// Type for TradesWith relationship
    5struct TradesWith { };
    6
    7int main(int, char*[]) {
    8 flecs::world ecs;
    9
    10 // Register TradesWith as symmetric relationship. Symmetric relationships
    11 // go both ways, adding (R, B) to A will also add (R, A) to B.
    12 ecs.component<TradesWith>()
    13 .add(flecs::Symmetric);
    14
    15 // Create two players
    16 flecs::entity player_1 = ecs.entity();
    17 flecs::entity player_2 = ecs.entity();
    18
    19 // Add (TradesWith, player_2) to player_1. This also adds
    20 // (TradesWith, player_1) to player_2.
    21 player_1.add<TradesWith>(player_2);
    22
    23 // Log platoon of unit
    24 std::cout << "Player 1 trades with Player 2: " <<
    25 (player_1.has<TradesWith>(player_2) ? "true" : "false") << "\n";
    26 std::cout << "Player 2 trades with Player 1: " <<
    27 (player_2.has<TradesWith>(player_1) ? "true" : "false") << "\n";
    28
    29 // Output:
    30 // Player 1 trades with Player 2: true
    31 // Player 2 trades with Player 1: true
    32}
    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