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(cocos): getComponent finds a component by a base class it derives from, so a handler bound through the Button of a Toggle's node reaches it

yhh2d87c03cc2af
diff --git a/sdk/src/cocos/node.ts b/sdk/src/cocos/node.ts
index 00dd89cae..94914d7b2 100644
--- a/sdk/src/cocos/node.ts
+++ b/sdk/src/cocos/node.ts
@@ -17,7 +17,7 @@ import type { World } from '../ecs/world';
import { INVALID_ENTITY, type Entity } from '../types';
import { Quat, Vec3 } from './math';
import {
- classInfoNamed, classInfoOf, ensureInstance, instancesOn, nodeOf, setNodeFactory, syncActivity,
+ COMPONENT_PREFIX, classInfoNamed, classInfoOf, ensureInstance, instancesOn, nodeOf, setNodeFactory, syncActivity,
type ComponentCtor,
} from './registry';
import type { Component } from './component';
@@ -328,16 +328,39 @@ export class Node {
const ctor = this.resolveCtor_(type);
if (ctor) {
const info = classInfoOf(ctor);
- if (info) return this._world.has(this._entity, info.def) ? (ensureInstance(this._world, this._entity, ctor) as T | null) : null;
+ if (info) {
+ if (this._world.has(this._entity, info.def)) return ensureInstance(this._world, this._entity, ctor) as T | null;
+ // the component asked for may be a base of the one the node holds (a Button on a node that has a Toggle, which is one)
+ return this.subclassInstances_(ctor)[0] as T | undefined ?? null;
+ }
}
return (builtinAdapterFor(type)?.get(this._world, this._entity) as T | null | undefined) ?? null;
}
getComponents<T extends Component>(type: ComponentCtor<T> | string): T[] {
+ const ctor = this.resolveCtor_(type);
+ const info = ctor ? classInfoOf(ctor) : undefined;
+ if (ctor && info) {
+ const own = this._world.has(this._entity, info.def) ? [ensureInstance(this._world, this._entity, ctor) as T] : [];
+ return [...own, ...(this.subclassInstances_(ctor) as T[])];
+ }
const one = this.getComponent<T>(type);
return one ? [one] : [];
}
+ /** The script components on this node whose class derives from @p base, other than @p base itself. */
+ private subclassInstances_(base: ComponentCtor): Component[] {
+ const out: Component[] = [];
+ for (const type of this._world.getComponentTypes(this._entity)) {
+ if (!type.startsWith(COMPONENT_PREFIX)) continue;
+ const info = classInfoNamed(type.slice(COMPONENT_PREFIX.length));
+ if (!info || info.ctor === base || !(info.ctor.prototype instanceof base)) continue;
+ const inst = ensureInstance(this._world, this._entity, info.ctor);
+ if (inst) out.push(inst);
+ }
+ return out;
+ }
+
getComponentInChildren<T extends Component>(type: ComponentCtor<T> | string): T | null {
const own = this.getComponent<T>(type);
if (own) return own;
diff --git a/sdk/tests/cocos/controls-wasm.test.ts b/sdk/tests/cocos/controls-wasm.test.ts
index 2ba2f5b36..deea2c095 100644
--- a/sdk/tests/cocos/controls-wasm.test.ts
+++ b/sdk/tests/cocos/controls-wasm.test.ts
@@ -102,6 +102,18 @@ describe.skipIf(!HAS_WASM)('toggles, bars, sliders and scroll views', () => {
expect(group.anyTogglesChecked()).toBe(false);
});
+ it('is found when its base class is asked for, as a Toggle is a Button, and a click on it reaches a handler bound through the Button', async () => {
+ boot();
+ const { t, node } = await toggle('t', 200, 200);
+ expect(node.getComponent(Button)).toBe(t);
+ expect(node.getComponents(Button)).toEqual([t]);
+ const clicked: number[] = [];
+ node.on(Button.EventType.CLICK, () => clicked.push(1));
+ await tap(200, 200);
+ expect(clicked).toEqual([1]);
+ expect(t.isChecked).toBe(true);
+ });
+
it('belongs to the container its parent holds, and switching one on by assignment switches the others off', async () => {
boot();
const holder = box('group', 400, 400, 400, 100);