Function to_entity


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

template <typename Entity, typename Component>
Entity to_entity(const basic_registry< Entity > &reg, const Component &instance)


Returns the entity associated with a given component.

Currently, this function only works correctly with the default pool as it makes assumptions about how the components are laid out.
Template Parameters

Entity - A valid entity type (see entt_traits for more details).

Component - Type of component.


reg - A registry that contains the given entity and its components.

instance - A valid component instance.

The entity associated with the given component.

Mentioned in


Lines 150-162 in src/entt/entity/helper.hpp.

template<typename Entity, typename Component>
Entity to_entity(const basic_registry<Entity> &reg, const Component &instance) {
    const auto view = reg.template view<const Component>();
    const auto *addr = std::addressof(instance);

    for(auto it = view.rbegin(), last = view.rend(); it < last; it += ENTT_PACKED_PAGE) {
        if(const auto dist = (addr - std::addressof(view.template get<const Component>(*it))); dist >= 0 && dist < ENTT_PACKED_PAGE) {
            return *(it + dist);

    return entt::null;

Add Discussion

Log in to comment