logo

Babylon.js Market

Lifetime

core

Lifetime

1
Unlock

Install with the CLI:

bjs download Lifetime

Lifetime

A seconds counter that despawns its entity when it hits zero — the recycle clock behind every bullet, spark, and shard that's supposed to vanish on its own.

What it does

LifetimeComponent is pure data: a single remaining field, the seconds left before the entity should go away. LifetimeSystem (query: Lifetime, priority 40) walks every entity that carries one, subtracts dt from remaining each frame, and the instant a counter reaches zero calls world.removeEntity(entity). That's the whole loop.

The subtlety is what removeEntity does. For a plain entity it destroys it. For a pooled spawn — bullets, debris, muzzle sparks — removeEntity parks the entity back in its pool instead, ready to be handed out again. So the same countdown that despawns a one-off explosion is also the recycle clock for the whole gun/destroy chain: fire a round, it lives remaining seconds, then returns to the pool for the next shot. serialize() round-trips the one number, so a saved scene reloads mid-countdown exactly where it left off.

Use it in a scene

A scene is just data — a list of entities, each with its components. At startup the SceneLoader turns this JSON into a live world; edit the file and reload to rebuild it.

Build it from scratch with the bjs CLI:

  1. 1Scaffold a project
    npm create @babylonjsmarket/arcade@latest my-game

    World, renderer, and dev server — ready to run.

  2. 2Install dependencies
    cd my-game && npm install
  3. 3Add Lifetime
    bjs download Lifetime

    Copies its source into src/ so the scene resolves.

    First time? Run bjs login once.

  4. 4Paste the scene into src/scenes/arcade-room.ts and run
    npm run dev

    SceneLoader builds the world from the JSON; reload to rebuild.

JSON
{
  "Debris1": {
    "tags": ["debris"],
    "components": {
      "MeshPrimitive": { "primitive": "box", "height": 0.4 },
      "Lifetime": { "remaining": 2.5 }
    }
  }
}

Props

  • remaining (number, default 5) — seconds left on the clock. LifetimeSystem decrements it by dt each frame and removes the entity when it hits zero. Mutable at runtime: raise it to extend the window, drop it to cut a spawn short.

Events

  • Emits nothing. When the clock runs out the System calls world.removeEntity directly — the despawn (or pool release) is silent from Lifetime's side; any teardown or pool-return events belong to removeEntity, not here.
  • Listens for nothing. Entities are gathered by the query (required: [LifetimeComponent]), not off the bus.

Dependencies

  • None. Attach Lifetime to any entity that should vanish after N seconds — it reads nothing but its own counter and needs no companion component.

Notes

  • It runs mid-frame (priority 40). Systems that spawn or move entities should account for the fact that a Lifetime entity can be removed partway through the update order — don't cache a reference to one across frames without checking it's still active.
  • remaining is live. The tuning panel writes a new value straight onto every active Lifetime at once; you can do the same from your own code to stretch or cut a recycle window on the fly.
  • Pooled vs. destroyed is removeEntity's call, not Lifetime's. Whether zero means "recycle" or "gone for good" depends on how the entity was created (pooled spawn vs. plain createEntity). Lifetime just fires the trigger.

More like this

AiPilot
Animation
ArcCamera
Asteroid
Bullet
CameraFollow
DirectionalLight
Enemy
EnemySpawner
EnvironmentTexture
FreighterCar
FreighterChain
FreighterHead
GameConfig
Health
HemisphericLight
Jump
KeyboardInput
KeyboardMover
LineOfSight
MachineGun
Mesh
MeshPrimitive
Missile
MissileLauncher
MissionDirector
Movement
Obstacle
ObstacleField
OilBlob
Parts
Physics
PlayerInput
PlayerWalkAnimator
RespawnTimer
Score
Shadow
ShipLoadout
SkeletonAnimator
SpaceShooterBullet
SpaceShooterHealth
SpaceShooterScore
Transform6DOF
WaveDirector
Waypoint
WaypointTrack

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search