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(native): reading a component decomposes its world transform, as embind does

esengineb3ddc4e2dfe8
diff --git a/src/esengine/bindings/WebBindings.generated.cpp b/src/esengine/bindings/WebBindings.generated.cpp
index e41058837..cb36d0dd0 100644
--- a/src/esengine/bindings/WebBindings.generated.cpp
+++ b/src/esengine/bindings/WebBindings.generated.cpp
@@ -1791,9 +1791,9 @@ EMSCRIPTEN_BINDINGS(esengine_registry) {
auto entity = static_cast<Entity>(e);
static esengine::ecs::Transform s_dummy{};
if (!r.valid(entity) || !r.has<esengine::ecs::Transform>(entity)) return s_dummy;
- auto& t = r.get<esengine::ecs::Transform>(entity);
- t.ensureDecomposed();
- return t;
+ auto& c = r.get<esengine::ecs::Transform>(entity);
+ c.ensureDecomposed();
+ return c;
}), allow_raw_pointers())
.function("addTransform", optional_override([](Registry& r, u32 e, const esengine::ecs::Transform& c) {
auto entity = static_cast<Entity>(e);
diff --git a/tools/eht/data.py b/tools/eht/data.py
index 3949367dd..9ddf968e0 100644
--- a/tools/eht/data.py
+++ b/tools/eht/data.py
@@ -22,6 +22,15 @@ class Component:
header_path: str = ""
+# A C++ method every JS read of the component must run first. Transform's world
+# TRS is decomposed lazily from the cached matrix, so the fields sitting in memory
+# are stale until it is — a reader that skips this sees the last decomposed value
+# (zero for a UI node that nothing else drew). Declared once here because BOTH
+# binding surfaces expose the read: embind returns the struct, the native buffer
+# binding hands JS its memory, and the two must not drift.
+READ_HOOKS: Dict[str, str] = {'Transform': 'ensureDecomposed'}
+
+
@dataclass
class Enum:
"""An ES_ENUM enum class."""
diff --git a/tools/eht/generators/embind.py b/tools/eht/generators/embind.py
index f1a6337da..9e2bd5128 100644
--- a/tools/eht/generators/embind.py
+++ b/tools/eht/generators/embind.py
@@ -1,7 +1,7 @@
"""Emscripten embind C++ bindings generator."""
from typing import List, Set
-from ..data import Component, Enum
+from ..data import Component, Enum, READ_HOOKS
from ..type_system import TypeSystem
@@ -328,11 +328,18 @@ class EmbindGenerator:
lines.append(f' return r.has<{full}>(static_cast<Entity>(e));')
lines.append(' }))')
+ read_hook = READ_HOOKS.get(name)
+
if needs_wrap:
lines.append(f' .function("get{name}", optional_override([](Registry& r, u32 e) {{')
lines.append(f' auto entity = static_cast<Entity>(e);')
lines.append(f' if (!r.valid(entity) || !r.has<{full}>(entity)) return {js}{{}};')
- lines.append(f' return {to_js}(r.get<{full}>(entity));')
+ if read_hook:
+ lines.append(f' auto& c = r.get<{full}>(entity);')
+ lines.append(f' c.{read_hook}();')
+ lines.append(f' return {to_js}(c);')
+ else:
+ lines.append(f' return {to_js}(r.get<{full}>(entity));')
lines.append(' }))')
lines.append(f' .function("add{name}", optional_override([](Registry& r, u32 e, const {js}& js) {{')
lines.append(f' auto entity = static_cast<Entity>(e);')
@@ -344,10 +351,10 @@ class EmbindGenerator:
lines.append(f' auto entity = static_cast<Entity>(e);')
lines.append(f' static {full} s_dummy{{}};')
lines.append(f' if (!r.valid(entity) || !r.has<{full}>(entity)) return s_dummy;')
- if name == 'Transform':
- lines.append(f' auto& t = r.get<{full}>(entity);')
- lines.append(f' t.ensureDecomposed();')
- lines.append(f' return t;')
+ if read_hook:
+ lines.append(f' auto& c = r.get<{full}>(entity);')
+ lines.append(f' c.{read_hook}();')
+ lines.append(f' return c;')
else:
lines.append(f' return r.get<{full}>(entity);')
lines.append(' }), allow_raw_pointers())')
diff --git a/tools/eht/generators/native_bindings.py b/tools/eht/generators/native_bindings.py
index 539cdf1cf..b94019c19 100644
--- a/tools/eht/generators/native_bindings.py
+++ b/tools/eht/generators/native_bindings.py
@@ -13,7 +13,7 @@ for the ``esn_*`` readers, the entity lookup, and the component includes.
"""
from typing import List
-from ..data import Component, Enum
+from ..data import Component, Enum, READ_HOOKS
from ..type_system import TypeSystem
# Numeric primitives written straight through (via a double), by cleaned type.
@@ -78,6 +78,9 @@ class NativeBindingsGenerator:
out.append(' if (argc < 1) return JS_UNDEFINED;')
out.append(' esengine::Entity e = esn_entity(ctx, argv[0]);')
out.append(f' auto& c = esn_reg().getOrEmplace<{full}>(e);')
+ read_hook = READ_HOOKS.get(comp.name)
+ if read_hook:
+ out.append(f' c.{read_hook}(); // lazily derived fields, see READ_HOOKS')
out.append(f' return esn_arraybuffer(ctx, &c, sizeof({full}));')
out.append('}')
# Lifecycle: the native siblings of the embind Registry's has/remove. With