Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Script in onStart may cause some functions to not be executed correctly. #2102

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/core/src/ComponentsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,16 @@ export class ComponentsManager {
const onStartScripts = this._onStartScripts;
if (onStartScripts.length > 0) {
// The 'onStartScripts.length' maybe add if you add some Script with addComponent() in some Script's onStart()
onStartScripts.forEachAndClean((script: Script) => {
script._started = true;
this.removeOnStartScript(script);
script.onStart();
});
onStartScripts.forEachAndClean(
(script: Script) => {
script._started = true;
this.removeOnStartScript(script);
script.onStart();
},
(element: Script, index: number) => {
element._onStartIndex = index;
}
);
}
}

Expand Down
23 changes: 14 additions & 9 deletions packages/core/src/DisorderedArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,25 @@ export class DisorderedArray<T> {
this._endLoop(swapFn);
}

forEachAndClean(callbackFn: (e: T) => void): void {
forEachAndClean(callbackFn: (e: T) => void, swapFn: (element: T, index: number) => void): void {
this._startLoop();
const preEnd = this.length;
const elements = this._elements;
for (let i = 0, n = this.length; i < n; i++) {
for (let i = 0, n = preEnd; i < n; i++) {
const element = elements[i];
element && callbackFn(element);
}
this._endLoopAndClear();
let index = 0;
for (let i = preEnd, n = this.length; i < n; i++) {
const element = elements[i];
if (!element) continue;
elements[index] = element;
swapFn(element, index);
index++;
}
this._isLooping = false;
this.length = index;
this._blankCount = 0;
}

sort(compareFn: (a: T, b: T) => number): void {
Expand Down Expand Up @@ -127,10 +138,4 @@ export class DisorderedArray<T> {
this._blankCount = 0;
}
}

private _endLoopAndClear(): void {
this._isLooping = false;
this.length = 0;
this._blankCount = 0;
}
}
26 changes: 26 additions & 0 deletions tests/src/core/Script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ describe("Script", () => {
expect(script3.onUpdate).to.have.been.called.exactly(3);
});

it("Script add in script's onStart", async () => {
const engine = await WebGLEngine.create({ canvas: document.createElement("canvas") });
const scene = engine.sceneManager.activeScene;
class Script1 extends Script {
onStart(): void {
entity1.addComponent(Script2);
}
}
class Script2 extends Script {
onStart(): void {}
}

Script1.prototype.onStart = chai.spy(Script1.prototype.onStart);
Script2.prototype.onStart = chai.spy(Script2.prototype.onStart);

const entity1 = scene.createRootEntity("1");
const script1 = entity1.addComponent(Script1);
engine.update();
expect(script1.onStart).to.have.been.called.exactly(1);
const script2 = entity1.getComponent(Script2);
expect(script2.onStart).to.have.been.called.exactly(0);
engine.update();
expect(script1.onStart).to.have.been.called.exactly(1);
expect(script2.onStart).to.have.been.called.exactly(1);
});

it("Engine destroy outside the main loop", async () => {
class TestScript extends Script {
onAwake() {
Expand Down
Loading