RespawnTimer

The wait between a death and the respawn. Call start() when the player dies and update(dt) once per frame; update() returns true (and calls onRespawn()) on the frame the delay runs out. What a respawn does is up to the owner: reset the scene, send the player to a spawn point, refill health. Do it where update() returns true, or subclass and override onRespawn(). progress() can drive a fade or a countdown while waiting.

Properties

  • delaySeconds (float) — Seconds from start() to the respawn. Changing it mid-wait takes effect straight away.

Example

CODE
m.respawn = new BGE.RespawnTimer(2.0)
' When the player dies:
m.respawn.start()
' In onUpdate(dt):
if m.respawn.update(dt, not m.game.isTransitioning())
m.game.resetScene()
end if

Constructor

new RespawnTimer(delaySeconds?: float): RespawnTimer

Parameters

  • delaySeconds (float, optional, default: "3.0")

Instance Methods

start(restart?: boolean): void

Starts the wait. Does nothing if already waiting (so it's safe to call every frame while dead) unless restart is true, which starts the delay over.

Parameters

  • restart (boolean, optional, default: false)

Returns

  • void

cancel(): void

Stops waiting without respawning.

Returns

  • void

update(dt: float, ready?: boolean): boolean

Advances the wait. Once the delay has passed it respawns on the first update() where ready is true: the wait ends, onRespawn() is called, and this returns true.

Parameters

  • dt (float) — seconds
  • ready (boolean, optional, default: true) — false holds a due respawn, e.g. while a scene transition runs

Returns

  • boolean — true on the update that respawned

isWaiting(): boolean

Returns

  • boolean — whether it's waiting to respawn (including a due respawn held back by ready)

isDue(): boolean

Whether the delay has passed and the respawn is only waiting for ready.

Returns

  • boolean

elapsed(): float

Seconds since start() (0 when not waiting).

Returns

  • float

remaining(): float

Seconds left of the delay (never negative; 0 when not waiting).

Returns

  • float

progress(): float

0 as the wait starts, rising to 1 as the delay runs out, e.g. for a fade (0 when not waiting).

Returns

  • float

onRespawn(): void

protected

Returns

  • void