diff --git a/packages/core/src/Entity.ts b/packages/core/src/Entity.ts index adc2e769a7..47c0e6fa10 100644 --- a/packages/core/src/Entity.ts +++ b/packages/core/src/Entity.ts @@ -645,12 +645,12 @@ export class Entity extends EngineObject { activeChangeFlag & ActiveChangeFlag.Hierarchy && (this._isActiveInHierarchy = true); activeChangeFlag & ActiveChangeFlag.Scene && (this._isActiveInScene = true); const components = this._components; - for (let i = components.length - 1; i >= 0; i--) { + for (let i = 0, n = components.length; i < n; i++) { const component = components[i]; (component.enabled || !component._awoken) && activeChangedComponents.push(component); } const children = this._children; - for (let i = children.length - 1; i >= 0; i--) { + for (let i = 0, n = children.length; i < n; i++) { const child = children[i]; child.isActive && child._setActiveInHierarchy(activeChangedComponents, activeChangeFlag); } @@ -660,12 +660,12 @@ export class Entity extends EngineObject { activeChangeFlag & ActiveChangeFlag.Hierarchy && (this._isActiveInHierarchy = false); activeChangeFlag & ActiveChangeFlag.Scene && (this._isActiveInScene = false); const components = this._components; - for (let i = components.length - 1; i >= 0; i--) { + for (let i = 0, n = components.length; i < n; i++) { const component = components[i]; component.enabled && activeChangedComponents.push(component); } const children = this._children; - for (let i = children.length - 1; i >= 0; i--) { + for (let i = 0, n = children.length; i < n; i++) { const child = children[i]; child.isActive && child._setInActiveInHierarchy(activeChangedComponents, activeChangeFlag); } diff --git a/tests/src/core/Script.test.ts b/tests/src/core/Script.test.ts index 07e410d496..207270d891 100644 --- a/tests/src/core/Script.test.ts +++ b/tests/src/core/Script.test.ts @@ -321,5 +321,34 @@ describe("Script", () => { engine.update(); }).to.not.throw(); }); + + it("script order", async () => { + const engine = await WebGLEngine.create({ canvas: document.createElement("canvas") }); + const scene = engine.sceneManager.activeScene; + const rootEntity = scene.createRootEntity("root"); + const entity = rootEntity.createChild("entity"); + let tag: string; + entity.addComponent( + class extends Script { + onUpdate(deltaTime: number): void { + tag = "script1"; + } + } + ); + entity.addComponent( + class extends Script { + onUpdate(deltaTime: number): void { + tag = "script2"; + } + } + ); + engine.update(); + expect(tag).to.equal("script2"); + + rootEntity.removeChild(entity); + rootEntity.addChild(entity); + engine.update(); + expect(tag).to.equal("script2"); + }); }); });