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): a @ccclass class that is not a component is not driven as a script, so the frame loop no longer calls timers it does not have

yhh58281279782b
diff --git a/sdk/src/cocos/decorators.ts b/sdk/src/cocos/decorators.ts
index 9b0458057..7a9e2fe4c 100644
--- a/sdk/src/cocos/decorators.ts
+++ b/sdk/src/cocos/decorators.ts
@@ -102,6 +102,8 @@ export function property(...args: unknown[]): any {
/** `@ccclass('Name')` or bare `@ccclass`: declare the class, under its own name if none is given. */
export function ccclass(arg?: string | Function): any {
const declare = (ctor: Function, name?: string): void => {
+ // A class that is not a component (a plain data class, or one whose base the layer does not provide) is not driven: it has no hooks to run, and nothing to put on a node.
+ if (!(ctor.prototype instanceof Component)) return;
declareClass(ctor as never, name || ctor.name, propertiesOf(ctor));
};
if (typeof arg === 'function') { declare(arg); return undefined; }
diff --git a/sdk/tests/cocos/ccclass-plain.test.ts b/sdk/tests/cocos/ccclass-plain.test.ts
new file mode 100644
index 000000000..cb4debc4a
--- /dev/null
+++ b/sdk/tests/cocos/ccclass-plain.test.ts
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file `@ccclass` on a class that is not a component: Cocos allows it for serializable data, and the layer must not drive it as a script.
+ */
+import { describe, expect, it } from 'vitest';
+import { ccclass, classInfoOf, Component } from '../../src/cocos';
+
+describe('ccclass on a plain class', () => {
+ it('declares nothing for a class that does not derive from Component', () => {
+ @ccclass('PlainData')
+ class PlainData { value = 1; }
+ @ccclass('RealScript')
+ class RealScript extends Component {}
+ expect(classInfoOf(PlainData)).toBeUndefined();
+ expect(classInfoOf(RealScript)).toBeDefined();
+ expect(new PlainData().value).toBe(1);
+ });
+});