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): a component's entity list reaches the native core, so a spring bone's colliders work in a device build and a script can read them

yhh9d648fb60059
diff --git a/sdk/src/ecs/component.generated.ts b/sdk/src/ecs/component.generated.ts
index 7cb12d46d..595d56220 100644
--- a/sdk/src/ecs/component.generated.ts
+++ b/sdk/src/ecs/component.generated.ts
@@ -1138,6 +1138,7 @@ export const COMPONENT_META: Record<string, ComponentMetaEntry> = {
},
assetFields: [],
entityFields: ['colliders'],
+ listFields: ['colliders'],
colorFields: [],
animatableFields: [],
fields: {
diff --git a/tools/eht/generators/metadata.py b/tools/eht/generators/metadata.py
index 390dd629a..453496076 100644
--- a/tools/eht/generators/metadata.py
+++ b/tools/eht/generators/metadata.py
@@ -70,6 +70,12 @@ class MetadataGenerator:
t = self.types.clean_type(prop.cpp_type)
return t in self.types.VECTOR_TYPES and 'entity_ref' not in prop.annotations
+ def _is_entity_list(self, comp: Component, prop) -> bool:
+ """A list of entities crossing as a generated pair, as other lists do on the native core."""
+ t = self.types.clean_type(prop.cpp_type)
+ return (t in self.types.VECTOR_TYPES and 'entity_ref' in prop.annotations
+ and comp.name not in self.types.HAND_WIRED_ENTITY_LISTS)
+
def _get_entity_fields(self, comp: Component) -> List[str]:
return [p.name for p in comp.properties if 'entity_ref' in p.annotations]
@@ -529,7 +535,8 @@ class MetadataGenerator:
list_fields = [p.name for p in comp.properties
if self.types.is_struct_vector(p.cpp_type)
- or self._is_number_list(p)]
+ or self._is_number_list(p)
+ or self._is_entity_list(comp, p)]
if list_fields:
parts = ', '.join(f"'{f}'" for f in list_fields)
lines.append(f' listFields: [{parts}],')
diff --git a/tools/eht/generators/native_bindings.py b/tools/eht/generators/native_bindings.py
index 422bc2220..8d15e9d12 100644
--- a/tools/eht/generators/native_bindings.py
+++ b/tools/eht/generators/native_bindings.py
@@ -108,15 +108,16 @@ class NativeBindingsGenerator:
A variable-length field has no place in the zero-copy component buffer the
ptr accessors write through — its storage is a pointer — so it crosses as
- its own pair of bindings. Entity lists predate this and keep the
- hand-written pair the registry wires them by (MeshSkin, Children).
+ its own pair of bindings. Two entity lists predate this and keep the
+ hand-written pair the registry wires them by (TypeSystem.HAND_WIRED_ENTITY_LISTS).
"""
out = []
for prop in comp.properties:
t = self.types.clean_type(prop.cpp_type)
# A list of plain numbers crosses as a list of numbers: no members to
# walk, which is what the empty list below means downstream.
- if t in self.types.VECTOR_TYPES and 'entity_ref' not in prop.annotations:
+ if t in self.types.VECTOR_TYPES and ('entity_ref' not in prop.annotations
+ or comp.name not in self.types.HAND_WIRED_ENTITY_LISTS):
out.append((prop, []))
continue
if not self.types.is_struct_vector(t):
@@ -144,7 +145,9 @@ class NativeBindingsGenerator:
out.append(f' esjs::set(o, "{m}", esjs::number(v.{m}));')
out.append(' esjs::setAt(arr, i++, o);')
else:
- out.append(' esjs::setAt(arr, i++, esjs::number(v));')
+ # An Entity converts to a number through its id, as the embind side writes it.
+ wide = 'static_cast<esengine::u32>(v)' if 'entity_ref' in prop.annotations else 'v'
+ out.append(f' esjs::setAt(arr, i++, esjs::number({wide}));')
out += [' }',
' return arr;',
'}',
@@ -162,7 +165,10 @@ class NativeBindingsGenerator:
for m in members:
out.append(f' v.{m} = static_cast<decltype(v.{m})>(esjs::toNumber(esjs::get(e, "{m}")));')
else:
- out.append(' v = static_cast<decltype(v)>(esjs::toNumber(e));')
+ if 'entity_ref' in prop.annotations:
+ out.append(' v = decltype(v)(static_cast<esengine::u32>(esjs::toNumber(e)));')
+ else:
+ out.append(' v = static_cast<decltype(v)>(esjs::toNumber(e));')
out += [f' c.{prop.name}.push_back(v);',
' }',
' return esjs::undefined();',
diff --git a/tools/eht/type_system.py b/tools/eht/type_system.py
index 87ac45172..a5ea42e1d 100644
--- a/tools/eht/type_system.py
+++ b/tools/eht/type_system.py
@@ -37,6 +37,11 @@ class TypeSystem:
# `std::vector<T>` fields the boundary marshals: element C++ type, the embind
# vector registered for it, and the TS interface that names it. Adding one is
# an entry here — every generator below reads the registry.
+ # Entity lists the native registry wires by hand, so the generators leave them out: a skin's joints go through
+ # their own binding, and Children is kept in step with Parent by the hierarchy system. Every other entity list
+ # crosses as a generated pair, or the native core silently has none of it.
+ HAND_WIRED_ENTITY_LISTS = {'Children', 'MeshSkin'}
+
VECTOR_TYPES = {
'std::vector<Entity>': ('u32', 'VectorEntity', 'VectorEntity'),
'std::vector<f32>': ('f32', 'VectorFloat', 'VectorFloat'),