From 709d1c93a49dff2d48b83b41730df5465d37e7b6 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 5 Mar 2020 23:16:05 +0100 Subject: [PATCH] registry: fixed a bug that affects late group initialization (close #436) --- TODO | 2 ++ src/entt/entity/registry.hpp | 10 +++++----- test/entt/entity/group.cpp | 13 +++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/TODO b/TODO index 47e1b847c..dff335fa1 100644 --- a/TODO +++ b/TODO @@ -16,6 +16,8 @@ * any-of rule for views/groups (eg entity has A and any of B/C/D) - get -> all, exclude -> none * review prepare after clone and the others have been removed +* !(has(e) || ...) -> !any(e) +* review pool::remove, ::assign * WIP: - deprecate snapshot, loader, ... diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index a54c8689c..36fcff3fe 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -1404,12 +1404,12 @@ public: handler->current.construct(entity); } } else { - const auto curr = view(exclude); - // we cannot iterate backwards because we want to leave behind valid entities in case of owned types - std::for_each(std::make_reverse_iterator(curr.end()), std::make_reverse_iterator(curr.begin()), [cpools, handler](const auto entity) { - if(const auto pos = handler->current; !(std::get<0>(cpools).index(entity) < ++handler->current)) { - (std::get> &>(cpools).swap(std::get> &>(cpools).data()[pos], entity), ...); + std::for_each(std::get<0>(cpools).data(), std::get<0>(cpools).data() + std::get<0>(cpools).size(), [this, cpools, handler](const auto entity) { + if(has..., std::decay_t...>(entity) && !any(entity)) { + if(const auto pos = handler->current; !(std::get<0>(cpools).index(entity) < ++handler->current)) { + (std::get> &>(cpools).swap(std::get> &>(cpools).data()[pos], entity), ...); + } } }); } diff --git a/test/entt/entity/group.cpp b/test/entt/entity/group.cpp index a7d883050..7a98ebb8a 100644 --- a/test/entt/entity/group.cpp +++ b/test/entt/entity/group.cpp @@ -1120,3 +1120,16 @@ TEST(OwningGroup, SignalRace) { ASSERT_EQ(registry.group(entt::get).size(), 1u); } + +TEST(OwningGroup, AfterFact) { + entt::registry registry; + + for(std::size_t i{}; i < 30u; ++i) { + auto entity = registry.create(); + if(!(i % 2u)) registry.assign(entity); + if(!(i % 3u)) registry.assign(entity); + } + + // thanks to @pgruenbacher for pointing out this corner case + ASSERT_EQ((registry.group().size()), 5u); +}