Skip to content
Flecs v4.1
relationships/basics

  • C

    View on GitHub

    1#include <basics.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, Eats);
    8
    9 // Entity used for Grows relationship
    10 ecs_entity_t grows = ecs_entity(ecs, { .name = "Grows" });
    11
    12 // Relationship objects
    13 ecs_entity_t apples = ecs_entity(ecs, { .name = "Apples" });
    14 ecs_entity_t pears = ecs_entity(ecs, { .name = "Pears" });
    15
    16 // Create an entity with 3 relationships. Relationships are like regular components,
    17 // but combine two types/identifiers into an (relationship, object) pair.
    18 ecs_entity_t bob = ecs_entity(ecs, { .name = "Bob" });
    19 // Pairs can be constructed from a type and entity
    20 ecs_add_pair(ecs, bob, Eats, apples);
    21 ecs_add_pair(ecs, bob, Eats, pears);
    22 // Pairs can also be constructed from two entity ids
    23 ecs_add_pair(ecs, bob, grows, pears);
    24
    25 // Has can be used with relationships as well
    26 printf("Bob eats apples? %d\n", ecs_has_pair(ecs, bob, Eats, apples));
    27
    28 // Wildcards can be used to match relationships
    29 printf("Bob grows food? %d\n", ecs_has_pair(ecs, bob, grows, EcsWildcard));
    30
    31 // Print the type of the entity. Should output:
    32 // (Identifier,Name),(Eats,Apples),(Eats,Pears),(Grows,Pears)
    33 char *type_str = ecs_type_str(ecs, ecs_get_type(ecs, bob));
    34 printf("Bob's type: [%s]\n", type_str);
    35 ecs_os_free(type_str);
    36
    37 // Get first target of relationship
    38 printf("Bob eats %s\n",
    39 ecs_get_name(ecs, ecs_get_target(ecs, bob, Eats, 0)));
    40
    41 // Get second target of relationship
    42 printf("Bob also eats %s\n",
    43 ecs_get_name(ecs, ecs_get_target(ecs, bob, Eats, 1)));
    44
    45 return ecs_fini(ecs);
    46}
    const ecs_entity_t EcsWildcard
    Wildcard 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_get_target(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t rel, int32_t index)
    Get the target of a relationship.
    const ecs_type_t * ecs_get_type(const ecs_world_t *world, ecs_entity_t entity)
    Get the type of an entity.
    char * ecs_type_str(const ecs_world_t *world, const ecs_type_t *type)
    Convert a type to a string.
    #define ecs_add_pair(world, subject, first, second)
    Add a pair to an entity.
    Definition flecs_c.h:275
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #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
    const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
    Get the name of an entity.
    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 <basics.h>
    2#include <iostream>
    3
    4// Type used for Eats relationship
    5struct Eats { };
    6
    7int main(int, char*[]) {
    8 flecs::world ecs;
    9
    10 // Entity used for Grows relationship
    11 flecs::entity grows = ecs.entity("Grows");
    12
    13 // Relationship objects
    14 flecs::entity apples = ecs.entity("Apples");
    15 flecs::entity pears = ecs.entity("Pears");
    16
    17 // Create an entity with 3 relationships. Relationships are like regular components,
    18 // but combine two types/identifiers into an (relationship, object) pair.
    19 flecs::entity bob = ecs.entity("Bob")
    20 // Pairs can be constructed from a type and entity
    21 .add<Eats>(apples)
    22 .add<Eats>(pears)
    23 // Pairs can also be constructed from two entity ids
    24 .add(grows, pears);
    25
    26 // Has can be used with relationships as well
    27 std::cout << "Bob eats apples? " << bob.has<Eats>(apples) << "\n";
    28
    29 // Wildcards can be used to match relationships
    30 std::cout << "Bob grows food? " << bob.has(grows, flecs::Wildcard) << "\n";
    31
    32 // Print the type of the entity. Should output:
    33 // (Identifier,Name),(Eats,Apples),(Eats,Pears),(Grows,Pears)
    34 std::cout << "Bob's type: [" << bob.type().str() << "]\n";
    35
    36 // Relationships can be iterated for an entity. This iterates (Eats, *):
    37 bob.each<Eats>([](flecs::entity second) {
    38 std::cout << "Bob eats " << second.name() << "\n";
    39 });
    40
    41 // Iterate by explicitly providing the pair. This iterates (*, Pears):
    42 bob.each(flecs::Wildcard, pears, [](flecs::id id) {
    43 std::cout << "Bob " << id.first().name() << " pears\n";
    44 });
    45
    46 // Get first target of relationship
    47 std::cout << "Bob eats " << bob.target<Eats>().name() << "\n";
    48
    49 // Get second target of relationship
    50 std::cout << "Bob also eats " << bob.target<Eats>(1).name() << "\n";
    51}
    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
    void each(const Func &func) const
    Iterate (component) IDs of an entity.
    Definition impl.hpp:155
    flecs::entity target(int32_t index=0) const
    Get target for a given pair.
    Definition impl.hpp:36
    Entity.
    Definition entity.hpp:30
    Class that wraps around a flecs::id_t.
    Definition decl.hpp:27
    flecs::string str() const
    Convert the type to a comma-separated string.
    Definition type.hpp:38
    The world.
    Definition world.hpp:129