diff --git a/ecs.hpp b/ecs.hpp index cd30ce9..99b683e 100644 --- a/ecs.hpp +++ b/ecs.hpp @@ -1086,6 +1086,7 @@ namespace ecs_hpp template < typename T > std::size_t component_count() const noexcept; + std::size_t entity_count() const noexcept; std::size_t entity_component_count(const const_uentity& ent) const noexcept; template < typename F > @@ -1855,6 +1856,10 @@ namespace ecs_hpp : 0u; } + inline std::size_t registry::entity_count() const noexcept { + return entity_ids_.size(); + } + inline std::size_t registry::entity_component_count(const const_uentity& ent) const noexcept { assert(valid_entity(ent)); std::size_t component_count = 0u; diff --git a/ecs_tests.cpp b/ecs_tests.cpp index 53f3df6..3d00d26 100644 --- a/ecs_tests.cpp +++ b/ecs_tests.cpp @@ -411,6 +411,18 @@ TEST_CASE("registry") { // entity index overflow REQUIRE_THROWS_AS(w.create_entity(), std::logic_error); } + { + ecs::registry w; + REQUIRE_FALSE(w.entity_count()); + auto e1 = w.create_entity(); + REQUIRE(w.entity_count() == 1u); + auto e2 = w.create_entity(); + REQUIRE(w.entity_count() == 2u); + e1.destroy(); + REQUIRE(w.entity_count() == 1u); + e2.destroy(); + REQUIRE_FALSE(w.entity_count()); + } } SECTION("components") { {