Function get_or_emplace

Synopsis

#include <src/entt/entity/registry.hpp>

template <typename Component, typename... Args>
decltype(auto) get_or_emplace(const entity_type entity, Args &&... args)

Description

Returns a reference to the given component for an entity.

In case the entity doesn't own the component, the parameters provided are used to construct it.
Equivalent to the following snippet (pseudocode):

auto &component = registry.has<Component>(entity) ? registry.get<Component>(entity) : registry.emplace<Component>(entity, args...);

Prefer this function anyway because it has slightly better performance.

Warning
Attempting to use an invalid entity results in undefined behavior.
Template Parameters

Component - Type of component to get.

Args - Types of arguments to use to construct the component.

Parameters

entity - A valid entity identifier.

args - Parameters to use to initialize the component.

Return
Reference to the component owned by the entity.

Source

Lines 857-862 in src/entt/entity/registry.hpp.

template<typename Component, typename... Args>
[[nodiscard]] decltype(auto) get_or_emplace(const entity_type entity, Args &&... args) {
    ENTT_ASSERT(valid(entity));
    auto &cpool = assure<Component>();
    return cpool.contains(entity) ? cpool.get(entity) : cpool.emplace(*this, entity, std::forward<Args>(args)...);
}





Add Discussion

Log in to comment