Health

Hit points: current and max, with an optional invulnerability window after each hit. damage() says whether the hit killed. Flashes, knockback and sounds stay with the entity, or go in the onDamaged()/onDeath() hooks of a subclass. Call update(dt) once per frame if you use invulnerableSeconds.

Properties

  • max (float)
  • current (float)
  • invulnerableSeconds (float) — After each hit that lands, further hits are ignored for this long. 0 for none.

Example

CODE
m.health = new BGE.Health(6)
m.health.invulnerableSeconds = 1.0
' When something hits the player:
if m.health.canTakeDamage()
if m.health.damage(1)
m.startDefeat()
end if
end if
' In onUpdate(dt):
m.health.update(dt)

Constructor

new Health(max?: float): Health

Starts at full health.

Parameters

  • max (float, optional, default: 100)

Instance Methods

isAlive(): boolean

Returns

  • boolean — whether there's any health left

canTakeDamage(): boolean

Whether damage() would land: alive and not invulnerable.

Returns

  • boolean

damage(amount: float): boolean

Takes amount, never below zero, and starts the invulnerability window. Ignored while dead or invulnerable (see canTakeDamage()).

Parameters

  • amount (float)

Returns

  • boolean — true if this hit killed

heal(amount: float): void

Adds amount, up to max. Doesn't bring back the dead; use refill() for that.

Parameters

  • amount (float)

Returns

  • void

refill(): void

Back to full health, with no invulnerability left.

Returns

  • void

reset(max: float): void

Sets a new max and refills.

Parameters

  • max (float)

Returns

  • void

fraction(): float

current / max, 0 to 1.

Returns

  • float

makeInvulnerable(seconds: float): void

Ignores hits for seconds (a dodge, or a respawn), replacing any window running.

Parameters

  • seconds (float)

Returns

  • void

isInvulnerable(): boolean

Returns

  • boolean — whether hits are being ignored right now

invulnerableRemaining(): float

Seconds of invulnerability left, e.g. to blink the sprite.

Returns

  • float

update(dt: float): void

Counts down the invulnerability window. Call once per frame.

Parameters

  • dt (float) — seconds

Returns

  • void

onDamaged(amount: float): void

protected

Parameters

  • amount (float)

Returns

  • void

onDeath(): void

protected

Returns

  • void