Function to_entity

Summary

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

(1) template <typename Entity>
    constexpr entt_traits< Entity >::entity_type to_entity(const Entity value)

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

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

Function overload

Synopsis

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

template <typename Entity>
constexpr entt_traits< Entity >::entity_type to_entity(const Entity value)

Description

Returns the entity part once converted to the underlying type.

Parameters

value - The value to convert.

Returns
The integral representation of the entity part.
Template Parameters

Entity - The value type.

Mentioned in

Source

Lines 147-150 in src/entt/entity/entity.hpp.

template<typename Entity>
[[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_entity(const Entity value) ENTT_NOEXCEPT {
    return entt_traits<Entity>::to_entity(value);
}

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 139-152 in src/entt/entity/helper.hpp.

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

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

    return null;
}





Add Discussion

Log in to comment