t1k:cocos:playable:lifecycle
| Field | Value |
|---|---|
| Module | playable |
| Version | 0.5.6 |
| Effort | high |
| Tools | — |
Keywords: ads, cocos, component, GameLifecycleManager, lifecycle, playable
How to invoke
Section titled “How to invoke”/t1k:cocos:playable:lifecycleGameLifecycleManager — Non-Component Lifecycle System
Section titled “GameLifecycleManager — Non-Component Lifecycle System”Drives Tick/FixedTick/LateTick on plain TS classes that don’t extend Cocos Component. Add GameLifecycleManager as a component on a persistent scene node. See also: t1k-cocos-playable-signalbus, t1k-cocos-playable-async-utilities.
Import paths:
db://assets/PLAGameFoundation/gameLifecycle/GameLifecycleManagerdb://assets/PLAGameFoundation/gameLifecycle/LifecycleDecoratordb://assets/PLAGameFoundation/gameLifecycle/interfaces/I*
Interfaces
Section titled “Interfaces”| Interface | Method signature | Called when |
|---|---|---|
IInitializable | Initialize(): void | Immediately on Register() |
ITickable | Tick(dt: number): void | Every frame (via Cocos update) |
IFixedTickable | FixedTick(fixedDt: number): void | Fixed timestep accumulator (default 0.02s / 50 FPS) |
ILateTickable | LateTick(dt: number): void | After all Ticks (via Cocos lateUpdate) |
IDisposable | Dispose(): void | On Unregister() or scene destroy |
A class can implement any combination. Register() detects which interfaces are present via duck-typing (isType checks for method name existence).
@RegisterLifecycle() Decorator
Section titled “@RegisterLifecycle() Decorator”Auto-instantiates and registers the class when GameLifecycleManager.onLoad() runs. No-arg constructor required.
import { RegisterLifecycle } from "db://assets/PLAGameFoundation/gameLifecycle/LifecycleDecorator";import { IInitializable, ITickable, IDisposable } from "db://assets/PLAGameFoundation/gameLifecycle/interfaces/...";
@RegisterLifecycle()export class MySystem implements IInitializable, ITickable, IDisposable { Initialize(): void { // setup — called once at scene start }
Tick(dt: number): void { // runs every frame }
Dispose(): void { // cleanup on scene destroy }}Manual Registration
Section titled “Manual Registration”Use when you need constructor args or runtime control:
const system = new MySystem(someArg);GameLifecycleManager.Instance.Register(system);
// Later, to stop ticking and call Dispose:GameLifecycleManager.Instance.Unregister(system);Fixed Timestep
Section titled “Fixed Timestep”The manager accumulates delta time and calls FixedTick at a fixed rate:
// Default: 0.02s (50 Hz). Change at runtime:GameLifecycleManager.Instance.SetFixedTimeStep(1 / 30); // 30 HzGameLifecycleManager.Instance.GetFixedTimeStep();FixedTick receives the configured fixedDeltaTime, not the accumulated surplus.
isType Pattern (Internal)
Section titled “isType Pattern (Internal)”The manager uses duck-typing — not instanceof — to check interfaces:
// Internal implementation (for reference):private isType<T>(obj: any, methodName: string): obj is T { return methodName in obj;}This means TypeScript interfaces are checked at runtime by method presence. Ensure method names match exactly (Tick, FixedTick, LateTick, Initialize, Dispose).
Debug Info
Section titled “Debug Info”console.log(GameLifecycleManager.Instance.GetDebugInfo());// Prints counts for each collection + fixedTimeStepWhen to Use Lifecycle Manager vs Cocos Component update()
Section titled “When to Use Lifecycle Manager vs Cocos Component update()”| Scenario | Use |
|---|---|
| Pure logic / service class (no Node) | @RegisterLifecycle() + interfaces |
| Node-attached behaviour | Cocos Component with update() |
| Physics-like deterministic step | IFixedTickable |
| Camera follow / post-process | ILateTickable |
| One-time setup with no ticking | IInitializable only |
| Cross-system service needing frame updates | ITickable via lifecycle manager |
Common Mistakes
Section titled “Common Mistakes”- Implementing
tick()(lowercase) instead ofTick()— manager checks exact name; the method is silently ignored. - Using
@RegisterLifecycle()on a class with constructor arguments — manager callsnew ClassConstructor()with no args; use manualRegister()instead. - Multiple
GameLifecycleManagercomponents in the scene — the second one self-destructs. Keep exactly one on a persistent node. - Forgetting
IDisposableon systems holding signal subscriptions or timers — causes leaks on scene reload.
Gotchas
Section titled “Gotchas”- Cocos lifecycle order: onLoad → onEnable → start → update — wiring listeners in
startinstead ofonEnableskips the first re-enable cycle. onDisablefires on scene close beforeonDestroy— release resources inonDestroy, notonDisable, to survive scene navigation.updateis called even on disabled components in some Cocos versions — guard withthis.enabledor migrate tolateUpdate.