Skip to content
Flecs v4.1
entities/emplace

  • C++

    View on GitHub

    1#include <emplace.h>
    2#include <iostream>
    3
    4// The emplace method constructs a component directly in the storage vs. a
    5// regular set, which moves the value into an already constructed value in the
    6// storage. This reduces the overhead of creating a new component.
    7//
    8// Components that do not have a default constructor need to be created with
    9// emplace, as they can't be constructed automatically by flecs.
    10
    11struct NoDefaultCtor {
    12 NoDefaultCtor(double x_, double y_) : x(x_), y(y_) { }
    13
    14 double x, y;
    15};
    16
    17int main(int, char *[]) {
    18 flecs::world ecs;
    19
    20 flecs::entity e = ecs.entity()
    21 .emplace<NoDefaultCtor>(10.0, 20.0);
    22
    23 const NoDefaultCtor& c = e.get<NoDefaultCtor>();
    24 std::cout << "{" << c.x << ", " << c.y << "}" << "\n";
    25
    26 // Output
    27 // {10, 20}
    28}
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    Entity.
    Definition entity.hpp:30
    The world.
    Definition world.hpp:129