Function to_entity

Synopsis

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

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

Description

Returns the entity associated with a given component.

Warning
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.

Parameters

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

instance - A valid component instance.

Returns
The entity associated with the given component.

Mentioned in

Source

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