Skip to content
Flecs v4.1
prefabs/basics

  • C

    View on GitHub

    1#include <basics.h>
    2#include <stdio.h>
    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//
    8// Prefab instances are entities that have an IsA relationship to the prefab.
    9// The IsA relationship causes instances to inherit the components from the
    10// prefab. By default all instances for a prefab share its components.
    11//
    12// Inherited components save memory as they only need to be stored once for all
    13// prefab instances. They also speed up the creation of prefabs, as inherited
    14// components don't need to be copied to the instances.
    15//
    16// To get a private copy of a component, an instance can add it which is called
    17// an override. Overrides can be manual (by using add) or automatic (see the
    18// override example).
    19//
    20// If a prefab has children, adding the IsA relationship instantiates the prefab
    21// children for the instance (see hierarchy example).
    22
    23typedef struct { double value; } Defense;
    24
    25int main(int argc, char *argv[]) {
    26 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    27
    28 ECS_COMPONENT(ecs, Defense);
    29
    30 // Make Defense component inheritable. By default components are copied from
    31 // the instance to the prefab. An inherited component is only stored on the
    32 // prefab, and is shared across all instances.
    34
    35 // Create a SpaceShip prefab with a Defense component.
    36 ecs_entity_t SpaceShip = ecs_entity(ecs, { .name = "SpaceShip" });
    37 ecs_add_id(ecs, SpaceShip, EcsPrefab);
    38 ecs_set(ecs, SpaceShip, Defense, {50});
    39
    40 // Create a prefab instance
    41 ecs_entity_t inst = ecs_entity(ecs, { .name = "my_spaceship" });
    42 ecs_add_pair(ecs, inst, EcsIsA, SpaceShip);
    43
    44 // Because of the IsA relationship, the instance now shares the Defense
    45 // component with the prefab, and can be retrieved as a regular component:
    46 const Defense *d_inst = ecs_get(ecs, inst, Defense);
    47 printf("defense: %f\n", d_inst->value);
    48
    49 // Because the component is shared, changing the value on the prefab will
    50 // also change the value for the instance:
    51 ecs_set(ecs, SpaceShip, Defense, { 100 });
    52 printf("defense after set: %f\n", d_inst->value); // now prints 100
    53
    54 // Prefab components can be iterated like regular components:
    55 ecs_query_t *q = ecs_query(ecs, {
    56 .terms = {
    57 { .id = ecs_id(Defense) }
    58 }
    59 });
    60
    61 ecs_iter_t it = ecs_query_iter(ecs, q);
    62 while (ecs_query_next(&it)) {
    63 Defense *d = ecs_field(&it, Defense, 0);
    64 for (int i = 0; i < it.count; i ++) {
    65 printf("%s: defense: %f\n",
    66 ecs_get_name(ecs, it.entities[i]), d[i].value);
    67 }
    68 }
    69
    70 // Output:
    71 // defense: 50.000000
    72 // defense after set: 100.000000
    73 // my_spaceship: defense: 100.000000
    74
    75 return ecs_fini(ecs);
    76}
    77
    78
    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 EcsInherit
    Inherit component on instantiate.
    const ecs_entity_t EcsPrefab
    Tag added to prefab entities.
    const ecs_entity_t EcsOnInstantiate
    Relationship that specifies component inheritance behavior.
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    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
    #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_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_query(world,...)
    Shorthand for creating a query with ecs_query_init().
    Definition flecs_c.h:215
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    #define ecs_get(world, entity, T)
    Get a component.
    Definition flecs_c.h:339
    #define ecs_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
    Get the name of an entity.
    bool ecs_query_next(ecs_iter_t *it)
    Progress a query iterator.
    ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
    Create a query iterator.
    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.
    Iterator.
    Definition flecs.h:1183
    Queries are lists of constraints (terms) that match entities.
    Definition flecs.h:858

  • C++

    View on GitHub

    1#include <basics.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//
    8// Prefab instances are entities that have an IsA relationship to the prefab.
    9// The IsA relationship causes instances to inherit the components from the
    10// prefab. By default all instances for a prefab share its components.
    11//
    12// Inherited components save memory as they only need to be stored once for all
    13// prefab instances. They also speed up the creation of prefabs, as inherited
    14// components don't need to be copied to the instances.
    15//
    16// To get a private copy of a component, an instance can add it which is called
    17// an override. Overrides can be manual (by using add) or automatic (see the
    18// auto_override example).
    19//
    20// If a prefab has children, adding the IsA relationship instantiates the prefab
    21// children for the instance (see hierarchy example).
    22
    23struct Defense {
    24 double value;
    25};
    26
    27int main() {
    28 flecs::world ecs;
    29
    30 // Make Defense component inheritable. By default components are copied from
    31 // the instance to the prefab. An inherited component is only stored on the
    32 // prefab, and is shared across all instances.
    33 ecs.component<Defense>().add(flecs::OnInstantiate, flecs::Inherit);
    34
    35 // Create a SpaceShip prefab with a Defense component.
    36 flecs::entity SpaceShip = ecs.prefab("SpaceShip")
    37 .set<Defense>({ 50 });
    38
    39 // Create a prefab instance
    40 flecs::entity inst = ecs.entity("my_spaceship").is_a(SpaceShip);
    41
    42 // Because of the IsA relationship, the instance now shares the Defense
    43 // component with the prefab, and can be retrieved as a regular component:
    44 const Defense& d_inst = inst.get<Defense>();
    45 std::cout << "defense: " << d_inst.value << "\n";
    46
    47 // Because the component is shared, changing the value on the prefab will
    48 // also change the value for the instance:
    49 SpaceShip.set<Defense>({ 100 });
    50 std::cout << "defense after set: " << d_inst.value << "\n";
    51
    52 // Prefab components can be iterated like regular components:
    53 ecs.each([](flecs::entity e, Defense& d) {
    54 std::cout << e.path() << ": " << d.value << "\n";
    55 });
    56
    57 // Output:
    58 // defense: 50
    59 // defense after set: 100
    60 // ::my_spaceship: 100
    61}
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    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::string path(const char *sep="::", const char *init_sep="::") const
    Return the entity path.
    Entity.
    Definition entity.hpp:30
    The world.
    Definition world.hpp:129