Skip to content
Flecs v4.1
entities/cpp_traits

  • C++

    View on GitHub

    1#include <cpp_traits.h>
    2#include <iostream>
    3
    4// The dont_fragment member registers the component with the DontFragment
    5// trait, which stores the component in a sparse storage that doesn't fragment
    6// tables (see the ComponentTraits manual).
    7struct Position {
    8 static constexpr bool dont_fragment = true;
    9 double x, y;
    10};
    11
    12// The sparse member registers the component with the Sparse trait, which
    13// stores the component in a sparse storage. Unlike DontFragment, adding or
    14// removing a sparse component still fragments tables, but the component data
    15// stays at a stable address and is never moved between tables.
    16struct Velocity {
    17 static constexpr bool sparse = true;
    18 double x, y;
    19};
    20
    21// The on_instantiate member registers the component with an OnInstantiate
    22// policy. Mass is registered with (OnInstantiate, Inherit), which makes
    23// instances share the component with their prefab instead of copying it.
    24struct Mass {
    25 static constexpr auto on_instantiate = flecs::on_instantiate::inherit;
    26 double value;
    27};
    28
    29int main(int, char *[]) {
    30 flecs::world ecs;
    31
    32 // Traits are applied on component registration (implicit or explicit).
    33 flecs::entity pos = ecs.component<Position>();
    34 std::cout << "Position has DontFragment: "
    35 << pos.has(flecs::DontFragment) << "\n";
    36
    37 flecs::entity vel = ecs.component<Velocity>();
    38 std::cout << "Velocity has Sparse: "
    39 << vel.has(flecs::Sparse) << "\n";
    40
    41 // Because Mass is inheritable, instances share it with the prefab.
    42 flecs::entity base = ecs.prefab("Spaceship").set<Mass>({100});
    43 flecs::entity inst = ecs.entity("MySpaceship").is_a(base);
    44 std::cout << "MySpaceship owns Mass: " << inst.owns<Mass>() << "\n";
    45 std::cout << "MySpaceship mass: " << inst.get<Mass>().value << "\n";
    46
    47 inst.set<Position>({10, 20});
    48
    49 // Sparse components are used just like regular components. Because Velocity
    50 // is stored in a sparse set, get<Velocity>() returns a pointer that stays
    51 // valid even as the entity moves between tables.
    52 inst.set<Velocity>({1, 2});
    53 std::cout << "MySpaceship velocity: {"
    54 << inst.get<Velocity>().x << ", " << inst.get<Velocity>().y << "}\n";
    55
    56 // When all components in a query have the dont_fragment trait, the query
    57 // returns a flecs::sparse_query, which iterates the sparse component
    58 // storages directly and is faster than a regular query.
    59 flecs::sparse_query<Position> q = ecs.query<Position>();
    60
    61 q.each([](flecs::entity e, Position& p) {
    62 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}" << "\n";
    63 });
    64
    65 // The same goes for world.each() which also uses the optimized iteration
    66 // mode when all components have the dont_fragment trait:
    67
    68 ecs.each([](flecs::entity e, Position& p) {
    69 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}" << "\n";
    70 });
    71
    72 // Output
    73 // Position has DontFragment: 1
    74 // Velocity has Sparse: 1
    75 // MySpaceship owns Mass: 0
    76 // MySpaceship mass: 100
    77 // MySpaceship velocity: {1, 2}
    78 // MySpaceship: {10, 20}
    79 // MySpaceship: {10, 20}
    80}
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    conditional_t< sizeof...(Args)==0 &&_::is_sparse_query< Comps... >::value, flecs::sparse_query< Comps... >, flecs::query< Comps... > > query(Args &&... args) const
    Create a query.
    Definition impl.hpp:326
    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_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    Query that iterates sparse component storages directly.
    void each(Func &&func) const
    Iterate the query.
    The world.
    Definition world.hpp:129
    on_instantiate
    OnInstantiate policies that can be assigned to a component at compile time with the flecs::on_instant...
    Definition utils.hpp:194