Skip to content
Flecs v4.1
prefabs/slots

  • C

    View on GitHub

    1#include <slots.h>
    2#include <stdio.h>
    3
    4// Prefab hierarchies that use the non-fragmenting Parent storage automatically
    5// let applications resolve an instantiated child from its prefab child. This
    6// provides slot-like lookup without adding a relationship to either entity.
    7
    8int main(int argc, char *argv[]) {
    9 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    10
    11 // Create a prefab hierarchy with non-fragmenting Parent storage.
    12 ecs_entity_t SpaceShip = ecs_entity(ecs, { .name = "SpaceShip" });
    13 ecs_add_id(ecs, SpaceShip, EcsPrefab);
    14 ecs_entity_t Engine = ecs_new_w_parent(ecs, SpaceShip, "Engine");
    15
    16 ecs_entity_t Cockpit = ecs_new_w_parent(ecs, SpaceShip, "Cockpit");
    17
    18 ecs_entity_t PilotSeat = ecs_new_w_parent(ecs, Cockpit, "PilotSeat");
    19
    20 // Create a prefab instance.
    21 ecs_entity_t inst = ecs_entity(ecs, { .name = "my_spaceship" });
    22 ecs_add_pair(ecs, inst, EcsIsA, SpaceShip);
    23
    24 // Get the instantiated children from their prefab children. A nested child
    25 // is resolved from the corresponding instance parent.
    26 ecs_entity_t inst_engine = ecs_get_target(ecs, inst, Engine, 0);
    27 ecs_entity_t inst_cockpit = ecs_get_target(ecs, inst, Cockpit, 0);
    28 ecs_entity_t inst_seat = ecs_get_target(ecs, inst_cockpit, PilotSeat, 0);
    29
    30 printf("instance engine found: %s\n", inst_engine ? "true" : "false");
    31 printf("instance cockpit found: %s\n", inst_cockpit ? "true" : "false");
    32 printf("instance seat found: %s\n", inst_seat ? "true" : "false");
    33
    34 // Output:
    35 // instance engine found: true
    36 // instance cockpit found: true
    37 // instance seat found: true
    38
    39 return ecs_fini(ecs);
    40}
    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 EcsIsA
    Used to express inheritance relationships.
    const ecs_entity_t EcsPrefab
    Tag added to prefab entities.
    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.
    ecs_entity_t ecs_new_w_parent(ecs_world_t *world, ecs_entity_t parent, const char *name)
    Create child with Parent component.
    #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
    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 <slots.h>
    2#include <iostream>
    3
    4// Prefab hierarchies that use the non-fragmenting Parent storage automatically
    5// let applications resolve an instantiated child from its prefab child. This
    6// provides slot-like lookup without adding a relationship to either entity.
    7
    8int main() {
    9 flecs::world ecs;
    10
    11 // Create a prefab hierarchy with non-fragmenting Parent storage.
    12 flecs::entity SpaceShip = ecs.prefab("SpaceShip");
    13 flecs::entity Engine = ecs.prefab(flecs::Parent{SpaceShip}, "Engine");
    14
    15 flecs::entity Cockpit = ecs.prefab(
    16 flecs::Parent{SpaceShip}, "Cockpit");
    17
    18 flecs::entity PilotSeat = ecs.prefab(
    19 flecs::Parent{Cockpit}, "PilotSeat");
    20
    21 // Create a prefab instance.
    22 flecs::entity inst = ecs.entity("my_spaceship").is_a(SpaceShip);
    23
    24 // Get the instantiated children from their prefab children. A nested child
    25 // is resolved from the corresponding instance parent.
    26 flecs::entity inst_engine = inst.target(Engine);
    27 flecs::entity inst_cockpit = inst.target(Cockpit);
    28 flecs::entity inst_seat = inst_cockpit.target(PilotSeat);
    29
    30 std::cout << std::boolalpha;
    31 std::cout << "instance engine found: " << inst_engine.is_valid() << "\n";
    32 std::cout << "instance cockpit found: " << inst_cockpit.is_valid() << "\n";
    33 std::cout << "instance seat found: " << inst_seat.is_valid() << "\n";
    34
    35 // Output:
    36 // instance engine found: true
    37 // instance cockpit found: true
    38 // instance seat found: true
    39}
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::entity prefab(const char *name=nullptr) const
    Create a prefab that's associated with a type.
    Definition impl.hpp:18
    EcsParent Parent
    Built-in EcsParent type.
    Definition c_types.hpp:86
    const Self & is_a(entity_t second) const
    Shortcut for add(IsA, entity).
    Definition builder.hpp:122
    bool is_valid() const
    Check if entity is valid.
    flecs::entity target(int32_t index=0) const
    Get target for a given pair.
    Definition impl.hpp:36
    Entity.
    Definition entity.hpp:30
    The world.
    Definition world.hpp:129