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.

fix(ecs): eachLive promised every arity and delivered one

esengine39fc61840e72
diff --git a/src/esengine/ecs/View.hpp b/src/esengine/ecs/View.hpp
index 6b7bb9aa2..6ba4f097b 100644
--- a/src/esengine/ecs/View.hpp
+++ b/src/esengine/ecs/View.hpp
@@ -294,6 +294,31 @@ public:
}
}
+ /**
+ * @brief Like each(), but iterates the live dense array with no defensive copy.
+ * @warning The callback MUST NOT emplace/remove ANY of the iterated components —
+ * that reallocates or swap-pops the array being walked. Use for the
+ * read-only per-frame pass; use each() when the callback may mutate.
+ */
+ template<typename Func>
+ void eachLive(Func&& func) {
+ if (!smallest_) return;
+
+ const std::vector<Entity>& dense = smallest_->entities();
+ for (usize i = 0; i < dense.size(); ++i) {
+ Entity entity = dense[i];
+ if (!allHaveExceptSmallest(entity)) continue;
+
+ if constexpr (std::is_invocable_v<Func, Entity, Components&...>) {
+ func(entity, getByEntity<Components>(entity)...);
+ } else if constexpr (std::is_invocable_v<Func, Components&...>) {
+ func(getByEntity<Components>(entity)...);
+ } else {
+ func(entity);
+ }
+ }
+ }
+
private:
template<typename T>
T& getByEntity(Entity entity) {
@@ -402,6 +427,13 @@ public:
/** @copydoc get() */
const T& get(Entity entity) const { return pool_->get(entity); }
+ /**
+ * @brief Gets all components for an entity as a tuple
+ * @param entity The entity
+ * @return Tuple of component references
+ */
+ std::tuple<T&> getAll(Entity entity) { return std::tuple<T&>(pool_->get(entity)); }
+
/**
* @brief Checks if the view is empty
* @return True if no entities have this component
@@ -414,6 +446,12 @@ public:
*/
usize size() const { return pool_ ? pool_->size() : 0; }
+ /**
+ * @brief Upper bound on matching entities — exact here, since nothing filters.
+ * @return Entity count
+ */
+ usize sizeHint() const { return size(); }
+
/**
* @brief Executes a function for each entity
* @tparam Func Callback type