Games
Combat Systems
GDScript
Godot 4 Headless Verified
Godot 4 Health & Damage Component
Signal-driven Godot 4 GDScript HealthComponent node with invulnerability frames, armor mitigation, healing caps, defeat signals, and HUD bar binding.
Architecture & Runtime Preview
Godot 4.xGodot 4 Node component — attach HealthComponent as a child of any CharacterBody2D/3D and connect signals to HealthBarHUD.
- 1Hitbox -> HealthComponent.apply_damage(amount)
- 2Armor mitigation + i-frame guard -> clamp(hp, 0, max_hp)
- 3Signals: health_changed(current, max) & defeated() -> HealthBarHUD
Example Project Integration: Copy scripts into res://components/combat/ in any Godot 4 project.
Source Code (2 files)
class_name HealthComponent
extends Node
signal health_changed(current_health: int, max_health: int)
signal damaged(amount: int, remaining_health: int)
signal healed(amount: int, current_health: int)
signal defeated
@export var max_health: int = 100
@export var armor_reduction: int = 0
@export var invulnerability_seconds: float = 0.25
var current_health: int
var is_alive: bool = true
var _invulnerable_until_ms: int = 0
func _ready() -> void:
current_health = max_health
health_changed.emit(current_health, max_health)
func apply_damage(raw_amount: int) -> int:
if not is_alive or raw_amount <= 0:
return 0
var now_ms := Time.get_ticks_msec()
if now_ms < _invulnerable_until_ms:
return 0
var mitigated := maxi(1, raw_amount - armor_reduction)
current_health = maxi(0, current_health - mitigated)
_invulnerable_until_ms = now_ms + int(invulnerability_seconds * 1000.0)
damaged.emit(mitigated, current_health)
health_changed.emit(current_health, max_health)
if current_health == 0:
is_alive = false
defeated.emit()
return mitigated
func heal(amount: int) -> int:
if not is_alive or amount <= 0:
return 0
var before := current_health
current_health = mini(max_health, current_health + amount)
var restored := current_health - before
if restored > 0:
healed.emit(restored, current_health)
health_changed.emit(current_health, max_health)
return restored
class_name HealthBarHUD
extends ProgressBar
@export var health_component: HealthComponent
@export var label_node: Label
func _ready() -> void:
if health_component == null:
return
health_component.health_changed.connect(_on_health_changed)
_on_health_changed(health_component.current_health, health_component.max_health)
func _on_health_changed(current: int, maximum: int) -> void:
max_value = maximum
value = current
if label_node != null:
label_node.text = "%d / %d HP" % [current, maximum]
Installation & Usage
Copy health_component.gd and health_bar_hud.gd into res://components/combat/ in your Godot 4 project.
Add HealthComponent as a child node of your player or enemy scene, configure max_health and armor_reduction, and bind HealthBarHUD.health_component in the Inspector.