Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

test(ecs): a template API is only checked where something calls it

esenginee11781bdc83d
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 27c792ca8..7e8d128cb 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -26,7 +26,7 @@ env:
CPP_TESTS: >-
test_ecs test_events test_particle test_tilemap test_sparse_set
test_resource_pool test_shader_variant test_log_format test_registry_safety
- test_registry_ondestroy
+ test_registry_ondestroy test_view_api_matrix
test_shader_device test_draw_params test_texture_fb test_buffer_device
test_compressed_format test_batch_builder test_boundary_span test_webgpu_device
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d1f32356..5878af82b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,26 @@ published separately; it ships inside the editor.
### Fixed
+- **`registry.eachLive<A, B>` did not compile.** `Registry::each`/`eachLive` are
+ variadic, so both read as available at any arity — but `eachLive` existed only on
+ the single-component `View<T>`, and the multi-component `View<Components...>` had
+ only `each`, which copies the whole dense entity array on every call so the
+ callback may add or remove components. Nothing in the engine iterates several
+ components through a callback (every hot path uses range-for, already live), so
+ the combination was never instantiated and never failed to build — until a game's
+ own C++ system asked for it. `View<Components...>` now has `eachLive` with the
+ same contract as the single-component one, and a per-frame `Transform + Velocity`
+ pass over 5000 entities costs 1.33× less than `each` with no per-frame allocation
+ at all. `View<T>` also gained the `sizeHint()` and `getAll()` its multi-component
+ sibling already had, so a template can iterate a view without knowing its arity.
+
+- **The Registry's own example did not compile.** `for (auto [entity, pos, vel] :
+ registry.view<Position, Velocity>().each())` was in the class docs and again on
+ `view()`: `each()` takes a callback and returns void, so nothing about that line
+ is valid. A new harness instantiates every arity × entry point × callback shape
+ of the view API, which is what would have caught both this and `eachLive` — a
+ template API is only checked where something calls it.
+
- **A slept scene kept drawing its characters, and hiding an entity still missed
renderers.** 0.45.0 unified two of the three lists that answer "what draws
here"; the editor's eye derived a different thirteen, and neither runtime list
diff --git a/src/esengine/ecs/Registry.hpp b/src/esengine/ecs/Registry.hpp
index 10334e304..300e97e2c 100644
--- a/src/esengine/ecs/Registry.hpp
+++ b/src/esengine/ecs/Registry.hpp
@@ -58,9 +58,9 @@ namespace esengine::ecs {
* registry.emplace<Velocity>(player, 1.0f, 0.0f);
*
* // Query and iterate
- * for (auto [entity, pos, vel] : registry.view<Position, Velocity>().each()) {
+ * registry.each<Position, Velocity>([&](Entity e, Position& pos, Velocity& vel) {
* pos.x += vel.x * deltaTime;
- * }
+ * });
* @endcode
*/
class Registry {
@@ -413,9 +413,10 @@ public:
* auto& pos = registry.get<Position>(entity);
* }
*
- * // Multi-component view with structured bindings
- * for (auto [entity, pos, vel] : registry.view<Position, Velocity>().each()) {
- * pos.x += vel.x * dt;
+ * // Multi-component view: range-for yields the entities that have them all
+ * auto view = registry.view<Position, Velocity>();
+ * for (Entity e : view) {
+ * view.get<Position>(e).x += view.get<Velocity>(e).x * dt;
* }
* @endcode
*/
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index f40a64b58..ac0048558 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -38,6 +38,13 @@ add_executable(test_sparse_set
target_link_libraries(test_sparse_set PRIVATE esengine)
add_test(NAME test_sparse_set COMMAND test_sparse_set)
+# View API matrix: instantiates every arity × entry point × callback shape
+add_executable(test_view_api_matrix
+ ecs/test_view_api_matrix.cpp
+)
+target_link_libraries(test_view_api_matrix PRIVATE esengine)
+add_test(NAME test_view_api_matrix COMMAND test_view_api_matrix)
+
# ResourcePool tests
add_executable(test_resource_pool
resource/test_resource_pool.cpp
diff --git a/tests/ecs/test_view_api_matrix.cpp b/tests/ecs/test_view_api_matrix.cpp
new file mode 100644
index 000000000..1580442ad
--- /dev/null
+++ b/tests/ecs/test_view_api_matrix.cpp
@@ -0,0 +1,181 @@
+/**
+ * @file test_view_api_matrix.cpp — every (arity × entry point × callback shape)
+ * combination of the view API, instantiated.
+ *
+ * Registry::each/eachLive are variadic, so a member missing from one View
+ * specialization is not a compile error until something instantiates that exact
+ * combination. Nothing in the engine iterates multiple components through a
+ * callback — every hot path uses range-for — so `eachLive<A, B>` type-checked as
+ * valid for as long as no caller existed. The instantiations below ARE the guard;
+ * the CHECKs then pin what each combination does.
+ */
+#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
+#include <doctest.h>
+
+#include <esengine/ESEngine.hpp>
+#include <algorithm>
+#include <tuple>
+#include <vector>
+
+using esengine::Entity;
+using esengine::usize;
+using esengine::ecs::Registry;
+
+namespace test {
+
+struct A { int v = 0; };
+struct B { int v = 0; };
+struct C { int v = 0; };
+
+/** Entities carrying every one of Cs, in the order the view walks them. */
+template<typename... Cs>
+std::vector<Entity> walk(Registry& registry) {
+ std::vector<Entity> seen;
+ registry.each<Cs...>([&](Entity e, Cs&...) { seen.push_back(e); });
+ return seen;
+}
+
+/**
+ * Instantiates the full grid at one arity: both entry points (Registry, View),
+ * both iteration modes (each, eachLive) and all three callback shapes the
+ * is_invocable_v ladder claims to accept, plus the shape API a view must offer at
+ * every arity for generic code to compile against it.
+ */
+template<typename... Cs>
+usize instantiateGrid(Registry& registry) {
+ usize calls = 0;
+
+ registry.each<Cs...>([&](Entity, Cs&...) { ++calls; });
+ registry.each<Cs...>([&](Cs&...) { ++calls; });
+ registry.each<Cs...>([&](Entity) { ++calls; });
+ registry.eachLive<Cs...>([&](Entity, Cs&...) { ++calls; });
+ registry.eachLive<Cs...>([&](Cs&...) { ++calls; });
+ registry.eachLive<Cs...>([&](Entity) { ++calls; });
+
+ auto view = registry.view<Cs...>();
+ view.each([&](Entity, Cs&...) { ++calls; });
+ view.each([&](Cs&...) { ++calls; });
+ view.each([&](Entity) { ++calls; });
+ view.eachLive([&](Entity, Cs&...) { ++calls; });
+ view.eachLive([&](Cs&...) { ++calls; });
+ view.eachLive([&](Entity) { ++calls; });
+
+ (void)view.empty();
+ (void)view.sizeHint();
+ for (Entity e : view) {
+ std::tuple<Cs&...> all = view.getAll(e);
+ (void)all;
+ ++calls;
+ }
+ return calls;
+}
+
+} // namespace test
+
+TEST_CASE("every arity offers the same iteration API") {
+ Registry registry;
+ Entity abc = registry.create();
+ registry.emplace<test::A>(abc);
+ registry.emplace<test::B>(abc);
+ registry.emplace<test::C>(abc);
+
+ // 13 grid entries, one entity each: 12 callbacks + 1 range-for step.
+ CHECK_EQ(test::instantiateGrid<test::A>(registry), 13u);
+ CHECK_EQ((test::instantiateGrid<test::A, test::B>(registry)), 13u);
+ CHECK_EQ((test::instantiateGrid<test::A, test::B, test::C>(registry)), 13u);
+}
+
+TEST_CASE("eachLive visits exactly what each visits, in the same order") {
+ Registry registry;
+ std::vector<Entity> both;
+ for (int i = 0; i < 8; ++i) {
+ Entity e = registry.create();
+ registry.emplace<test::A>(e, test::A{i});
+ // Only every other entity gets B, so the view has to filter.
+ if (i % 2 == 0) {
+ registry.emplace<test::B>(e, test::B{i});
+ both.push_back(e);
+ }
+ }
+
+ std::vector<Entity> snapshot = test::walk<test::A, test::B>(registry);
+ std::vector<Entity> live;
+ registry.eachLive<test::A, test::B>([&](Entity e, test::A&, test::B&) { live.push_back(e); });
+
+ CHECK_EQ(snapshot, both);
+ CHECK_EQ(live, snapshot);
+}
+
+TEST_CASE("eachLive hands out references into the live pools") {
+ Registry registry;
+ Entity e = registry.create();
+ registry.emplace<test::A>(e, test::A{1});
+ registry.emplace<test::B>(e, test::B{2});
+
+ registry.eachLive<test::A, test::B>([](test::A& a, test::B& b) {
+ a.v += 10;
+ b.v += 10;
+ });
+
+ CHECK_EQ(registry.get<test::A>(e).v, 11);
+ CHECK_EQ(registry.get<test::B>(e).v, 12);
+}
+
+TEST_CASE("a view over a component nothing has visits nothing") {
+ Registry registry;
+ Entity e = registry.create();
+ registry.emplace<test::A>(e);
+
+ usize calls = 0;
+ registry.eachLive<test::A, test::C>([&](Entity, test::A&, test::C&) { ++calls; });
+ registry.each<test::A, test::C>([&](Entity, test::A&, test::C&) { ++calls; });
+
+ CHECK_EQ(calls, 0u);
+ CHECK(registry.view<test::A, test::C>().empty());
+}
+
+TEST_CASE("the smallest pool drives iteration whichever argument it is") {
+ Registry registry;
+ Entity shared = registry.create();
+ registry.emplace<test::A>(shared);
+ registry.emplace<test::B>(shared);
+ for (int i = 0; i < 32; ++i) registry.emplace<test::A>(registry.create());
+
+ std::vector<Entity> ab, ba;
+ registry.eachLive<test::A, test::B>([&](Entity e, test::A&, test::B&) { ab.push_back(e); });
+ registry.eachLive<test::B, test::A>([&](Entity e, test::B&, test::A&) { ba.push_back(e); });
+
+ CHECK_EQ(ab, std::vector<Entity>{shared});
+ CHECK_EQ(ba, ab);
+}
+
+TEST_CASE("each tolerates a callback that removes the iterated component") {
+ Registry registry;
+ std::vector<Entity> created;
+ for (int i = 0; i < 6; ++i) {
+ Entity e = registry.create();
+ registry.emplace<test::A>(e, test::A{i});
+ registry.emplace<test::B>(e, test::B{i});
+ created.push_back(e);
+ }
+
+ std::vector<Entity> seen;
+ registry.each<test::A, test::B>([&](Entity e, test::A&, test::B&) {
+ seen.push_back(e);
+ registry.remove<test::A>(e);
+ });
+
+ CHECK_EQ(seen, created);
+ CHECK(registry.view<test::A, test::B>().empty());
+}
+
+TEST_CASE("a single-component view answers the multi-component shape questions") {
+ Registry registry;
+ Entity e = registry.create();
+ registry.emplace<test::A>(e, test::A{7});
+
+ auto view = registry.view<test::A>();
+ CHECK_EQ(view.size(), 1u);
+ CHECK_EQ(view.sizeHint(), view.size());
+ CHECK_EQ(std::get<0>(view.getAll(e)).v, 7);
+}