diff --git a/docs/md/entity.md b/docs/md/entity.md index d5aedf803..72357e5f9 100644 --- a/docs/md/entity.md +++ b/docs/md/entity.md @@ -1207,9 +1207,11 @@ components with entities without knowing their actual type and even initialize them by copy if needed: ```cpp -registry.visit(entity, [other](auto &&storage) { - // create a copy of an entity component by component - storage.emplace(other, storage.get(entity)); +// create a copy of an entity component by component +registry.visit([entity, other](auto &&storage) { + if(storage.contains(entity)) { + storage.emplace(other, storage.get(entity)); + } }); ``` diff --git a/src/entt/entity/handle.hpp b/src/entt/entity/handle.hpp index 038bb7572..91ddf864a 100644 --- a/src/entt/entity/handle.hpp +++ b/src/entt/entity/handle.hpp @@ -272,7 +272,11 @@ struct basic_handle { */ template void visit(Func &&func) const { - reg->visit(entt, std::forward(func)); + reg->visit([func = std::forward(func), this](auto &&storage) { + if(storage.contains(entt)) { + func(storage); + } + }); } private: diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 1d730dfa0..9e6044106 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -1295,30 +1295,6 @@ public: assure().respect(assure()); } - /** - * @brief Visits an entity and returns the pools for its components. - * - * The signature of the function should be equivalent to the following: - * - * @code{.cpp} - * void(const basic_sparse_set &); - * @endcode - * - * Returned pools are those of the components owned by the entity. - * - * @tparam Func Type of the function object to invoke. - * @param entity A valid identifier. - * @param func A valid function object. - */ - template - void visit(entity_type entity, Func func) const { - for(auto &&curr: pools) { - if(curr.second->contains(entity)) { - func(*curr.second); - } - } - } - /** * @brief Visits a registry and returns the pools for its components. * diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index 52961e9ec..31c7e372f 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -1872,28 +1872,6 @@ TEST(Registry, Visit) { }); ASSERT_TRUE(hasType[0] && hasType[1] && hasType[2]); - - hasType[0] = hasType[1] = hasType[2] = false; - - registry.visit(entity, [&hasType](const auto &pool) { - hasType[0] = hasType[0] || (pool.type() == entt::type_id()); - hasType[1] = hasType[1] || (pool.type() == entt::type_id()); - hasType[2] = hasType[2] || (pool.type() == entt::type_id()); - }); - - ASSERT_TRUE(hasType[0] && !hasType[1] && hasType[2]); - - hasType[0] = hasType[2] = false; - - registry.visit(other, [&hasType](const auto &pool) { - hasType[0] = hasType[0] || (pool.type() == entt::type_id()); - hasType[1] = hasType[1] || (pool.type() == entt::type_id()); - hasType[2] = hasType[2] || (pool.type() == entt::type_id()); - }); - - ASSERT_TRUE(!hasType[0] && hasType[1] && !hasType[2]); - - hasType[1] = false; } TEST(Registry, ScramblingPoolsIsAllowed) { diff --git a/test/example/entity_copy.cpp b/test/example/entity_copy.cpp index 836a6f218..eccb7cb5b 100644 --- a/test/example/entity_copy.cpp +++ b/test/example/entity_copy.cpp @@ -22,9 +22,9 @@ TEST(Example, EntityCopy) { ASSERT_FALSE((registry.any_of(dst))); ASSERT_FALSE(custom.contains(dst)); - registry.visit(src, [src, dst](auto &&storage) { + registry.visit([src, dst](auto &&storage) { // discard chars because why not, this is just an example after all - if(storage.type() != entt::type_id()) { + if(storage.type() != entt::type_id() && storage.contains(src)) { storage.emplace(dst, storage.get(src)); } });