Skip to content
Flecs v4.1
entities/prefab

  • C++

    View on GitHub

    1#include <prefab.h>
    2#include <iostream>
    3
    4// Prefabs are entities that can be used as templates for other entities. They
    5// are created with a builtin Prefab tag, which by default excludes them from
    6// queries and systems.
    7
    8struct Attack { double value; };
    9struct Defense { double value; };
    10struct FreightCapacity { double value; };
    11struct ImpulseSpeed { double value; };
    12struct HasFTL { };
    13
    14struct Position { double x = 0; double y = 0; };
    15
    16int main(int, char *[]) {
    17 flecs::world ecs;
    18
    19 // Create a prefab hierarchy.
    20 flecs::entity spaceship = ecs.prefab("Spaceship")
    21 // Add components to prefab entity as usual
    22 .set<ImpulseSpeed>({50})
    23 .set<Defense>({50})
    24
    25 // By default components in an inheritance hierarchy are shared between
    26 // entities. The override function ensures that instances have a private
    27 // copy of the component.
    28 .auto_override<Position>();
    29
    30 flecs::entity freighter = ecs.prefab("Freighter")
    31 // Short for .add(flecs::IsA, spaceship). This ensures the entity
    32 // inherits all components from spaceship.
    33 .is_a(spaceship)
    34 .add<HasFTL>()
    35 .set<FreightCapacity>({100})
    36 .set<Defense>({100});
    37
    38 flecs::entity mammoth_freighter = ecs.prefab("MammothFreighter")
    39 .is_a(freighter)
    40 .set<FreightCapacity>({500})
    41 .set<Defense>({300});
    42
    43 ecs.prefab("Frigate")
    44 .is_a(spaceship)
    45 .add<HasFTL>()
    46 .set<Attack>({100})
    47 .set<Defense>({75})
    48 .set<ImpulseSpeed>({125});
    49
    50 // Create a regular entity from a prefab.
    51 // The instance will have a private copy of the Position component, because
    52 // of the override in the spaceship entity. All other components are shared.
    53 flecs::entity inst = ecs.entity("my_mammoth_freighter")
    54 .is_a(mammoth_freighter);
    55
    56 // Inspect the type of the entity. This outputs:
    57 // Position,(Identifier,Name),(IsA,MammothFreighter)
    58 std::cout << "Instance type: [" << inst.type().str() << "]\n";
    59
    60 // Even though the instance doesn't have a private copy of ImpulseSpeed, we
    61 // can still get it using the regular API (outputs 50)
    62 const ImpulseSpeed& ptr = inst.get<ImpulseSpeed>();
    63 std::cout << "Impulse speed: " << ptr.value << "\n";
    64
    65 // Prefab components can be iterated just like regular components:
    66 ecs.each([](flecs::entity e, ImpulseSpeed& is, Position& p) {
    67 p.x += is.value;
    68 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    69 });
    70}
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.
    Definition impl.hpp:368
    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
    const Self & is_a(entity_t second) const
    Shortcut for add(IsA, entity).
    Definition builder.hpp:122
    flecs::type type() const
    Get the entity's type.
    Definition impl.hpp:94
    flecs::string_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    flecs::string str() const
    Convert the type to a comma-separated string.
    Definition type.hpp:38
    The world.
    Definition world.hpp:129