Cocos Creator 3.8.5 (but was spotted on older version also).
@ccclass('Test')
export class Test extends Component {
protected onEnable(): void {
console.log('enabled called!');
this.scheduleOnce(() => {
this.enabled = true;
}, 1);
this.enabled = false;
}
}
enabled called! will be logged only once, if we set this.enabled = false; inside onEnable().
It looks strange why I need to disable it straight away, but I coded state machine where after swithing to an state it inside can switch to another one and it happens in onEnabled method:
@ccclass('FlowSpinningState')
export class FlowSpinningState<
ModuleT extends FlowModule = FlowModule
> extends FlowState<ModuleT> {
protected onEnable(): void {
const { parent } = this;
if (parent.isSpinResultReceived) {
this.stateMachine.setState(FlowModuleStates.SPIN_STOP);
}
}
}
inside setState() in disables previous state component.