Skip to content

Commit

Permalink
Fix component disable error when do enabled = false in onAwake me…
Browse files Browse the repository at this point in the history
…thod (#1915)

* fix: component set enable in awake method
  • Loading branch information
cptbtptpbcptdtptp authored Dec 22, 2023
1 parent 8200bfd commit 53c6f0a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/core/src/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,28 @@ export class Component extends EngineObject {
this._enabled = value;
if (this._entity._isActiveInScene) {
if (value) {
this._phasedActiveInScene = true;
this._onEnableInScene();
if (!this._phasedActiveInScene) {
this._phasedActiveInScene = true;
this._onEnableInScene();
}
} else {
this._phasedActiveInScene = false;
this._onDisableInScene();
if (this._phasedActiveInScene) {
this._phasedActiveInScene = false;
this._onDisableInScene();
}
}
}
if (this._entity.isActiveInHierarchy) {
if (value) {
this._phasedActive = true;
this._onEnable();
if (!this._phasedActive) {
this._phasedActive = true;
this._onEnable();
}
} else {
this._phasedActive = false;
this._onDisable();
if (this._phasedActive) {
this._phasedActive = false;
this._onDisable();
}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/src/core/Script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,31 @@ describe("Script", () => {
}, 1000);
});

it("Enable Disable components", async () => {
const engine = await WebGLEngine.create({ canvas: document.createElement("canvas") });
class KKK extends Script {
cmdStr = "";
onEnable(): void {
this.cmdStr += "A";
}
onDisable(): void {
this.cmdStr += "D";
}
}
const root = new Entity(engine);
const kkk = root.addComponent(KKK);
root.addComponent(
class extends Script {
onEnable(): void {
kkk.enabled = false;
kkk.enabled = true;
}
}
);
engine.sceneManager.activeScene.addRootEntity(root);
expect(kkk.cmdStr[0]).to.eql("A");
});

it("Dependent components", async () => {
@dependentComponents(Camera, DependentMode.CheckOnly)
class CheckScript extends Script {}
Expand Down

0 comments on commit 53c6f0a

Please sign in to comment.