Simple Health
Type: GeneralA simple health component to damage, heal and track the health of any entity it's attached to.
Setup
- Add it to any Scene
- Connect Signals
- Use Methods to track Health
Code
Released under CC0 1.0 licence (opens in a new tab): use, change and share it in any project, without asking and without giving credit.
GDScript
@icon("res://assets/icons/health.svg")
class_name HealthComponent extends Node
signal health_changed(old_value: float, new_value: float)
signal max_health_changed(old_value: float, new_value: float)
signal damaged(amount: float, source: Node)
signal healed(amount: float, source: Node)
signal died(source: Node)
@export_range(1.0, 1000.0, 1.0, "or_greater", "hide_slider", "suffix:HP")
var max_health: float = 100.0:
set(value):
var old: float = max_health
var ratio: float = get_health_ratio()
max_health = maxf(value, 1.0)
if max_health != old:
max_health_changed.emit(old, max_health)
if is_node_ready() and not _dead:
_set_health(max_health * ratio if preserve_health_ratio else _health)
## How current health reacts when [member max_health] changes at runtime.
## [br][br]
## [b]true[/b]: keep the health percentage. 50/100 -> max 200 = 100/200 or max 50 = 25/50.
## [br]
## [b]false[/b]: keep the health value, only clamp it to the new max. 50/100 -> max 200 = 50/200 or max 40 = 40/40.
## [br][br]
## No effect before [method Node._ready] or while dead.
## Emits [signal max_health_changed] first, then [signal health_changed] if health changes.
@export var preserve_health_ratio: bool = true
var _health: float = 0.0
var _dead: bool = false
func _ready() -> void:
_set_health(max_health)
# deal damage and return actual damage dealt
func take_damage(amount: float, source: Node = null) -> float:
assert(is_node_ready(), "HealthComponent: take_damage() called before _ready().")
if not can_take_damage(amount):
return 0.0
var dealt: float = minf(amount, _health)
var killed: bool = _set_health(_health - dealt)
damaged.emit(dealt, source)
if killed:
died.emit(source)
return dealt
func can_take_damage(amount: float) -> bool:
return not _dead and amount > 0.0
# heal and return actual health healed
func heal(amount: float, source: Node = null) -> float:
if _dead or amount <= 0.0:
return 0.0
var before: float = _health
_set_health(_health + amount)
var restored: float = _health - before
if restored > 0.0:
healed.emit(restored, source)
return restored
func kill(source: Node = null) -> void:
assert(is_node_ready(), "HealthComponent: kill() called before _ready().")
if _set_health(0.0):
died.emit(source)
func get_health() -> float:
return _health
func is_dead() -> bool:
return _dead
func get_health_ratio() -> float:
return _health / max_health
func _set_health(value: float) -> bool:
if _dead:
return false
var old: float = _health
_health = clampf(value, 0.0, max_health)
_dead = _health <= 0.0
var just_died: bool = _dead
if _health != old:
health_changed.emit(old, _health)
return just_diedUsage example
GDScript
@onready var health: HealthComponent = $HealthComponent
func _ready() -> void:
health.died.connect(_on_died)
func _on_died(source: Node) -> void:
queue_free()
#######################
# In a different script
func _on_body_entered(body: Node) -> void:
var health: HealthComponent = body.get_node_or_null("HealthComponent") as HealthComponent
if health:
health.take_damage(10.0, self)