Skip to content

Commit b9e89b3

Browse files
fix: script function order (#1904)
1 parent 6b37bba commit b9e89b3

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

packages/core/src/Entity.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,12 @@ export class Entity extends EngineObject {
645645
activeChangeFlag & ActiveChangeFlag.Hierarchy && (this._isActiveInHierarchy = true);
646646
activeChangeFlag & ActiveChangeFlag.Scene && (this._isActiveInScene = true);
647647
const components = this._components;
648-
for (let i = components.length - 1; i >= 0; i--) {
648+
for (let i = 0, n = components.length; i < n; i++) {
649649
const component = components[i];
650650
(component.enabled || !component._awoken) && activeChangedComponents.push(component);
651651
}
652652
const children = this._children;
653-
for (let i = children.length - 1; i >= 0; i--) {
653+
for (let i = 0, n = children.length; i < n; i++) {
654654
const child = children[i];
655655
child.isActive && child._setActiveInHierarchy(activeChangedComponents, activeChangeFlag);
656656
}
@@ -660,12 +660,12 @@ export class Entity extends EngineObject {
660660
activeChangeFlag & ActiveChangeFlag.Hierarchy && (this._isActiveInHierarchy = false);
661661
activeChangeFlag & ActiveChangeFlag.Scene && (this._isActiveInScene = false);
662662
const components = this._components;
663-
for (let i = components.length - 1; i >= 0; i--) {
663+
for (let i = 0, n = components.length; i < n; i++) {
664664
const component = components[i];
665665
component.enabled && activeChangedComponents.push(component);
666666
}
667667
const children = this._children;
668-
for (let i = children.length - 1; i >= 0; i--) {
668+
for (let i = 0, n = children.length; i < n; i++) {
669669
const child = children[i];
670670
child.isActive && child._setInActiveInHierarchy(activeChangedComponents, activeChangeFlag);
671671
}

tests/src/core/Script.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,34 @@ describe("Script", () => {
321321
engine.update();
322322
}).to.not.throw();
323323
});
324+
325+
it("script order", async () => {
326+
const engine = await WebGLEngine.create({ canvas: document.createElement("canvas") });
327+
const scene = engine.sceneManager.activeScene;
328+
const rootEntity = scene.createRootEntity("root");
329+
const entity = rootEntity.createChild("entity");
330+
let tag: string;
331+
entity.addComponent(
332+
class extends Script {
333+
onUpdate(deltaTime: number): void {
334+
tag = "script1";
335+
}
336+
}
337+
);
338+
entity.addComponent(
339+
class extends Script {
340+
onUpdate(deltaTime: number): void {
341+
tag = "script2";
342+
}
343+
}
344+
);
345+
engine.update();
346+
expect(tag).to.equal("script2");
347+
348+
rootEntity.removeChild(entity);
349+
rootEntity.addChild(entity);
350+
engine.update();
351+
expect(tag).to.equal("script2");
352+
});
324353
});
325354
});

0 commit comments

Comments
 (0)