Created
March 14, 2025 14:04
-
-
Save viniciusemferreira/32ddf10aa6f80d17835b6fc2eaacf95d to your computer and use it in GitHub Desktop.
The HealthBarComponent script for Godot 4 is a UI component that visually represents an entity’s health using a ProgressBar. It connects to a HealthComponent, automatically updating when health changes. The bar dynamically reflects the entity’s remaining health as a percentage. This script is ideal for characters, enemies, or any game object tha…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name HealthBarComponent | |
extends Control | |
## The HealthComponent this health bar represents. | |
@export var health_component: HealthComponent | |
## Reference to the ProgressBar node inside the health bar. | |
@onready var progress_bar: ProgressBar = $ProgressBar | |
## Called when the node is added to the scene. | |
## Connects the health change signal to update the health bar. | |
func _ready() -> void: | |
if not health_component: | |
return # Exit if there is no assigned health component. | |
# Connect the health_changed signal to the update function. | |
health_component.health_changed.connect(update) | |
# Update the health bar once at the beginning. | |
update() | |
## Updates the progress bar's value to match the current health percentage. | |
func update() -> void: | |
progress_bar.value = health_component.get_percentage_health() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment