π Health Bar System Code Documentation
This document details the Health Bar system coded in GDScript for indie game development.
Overview
res://common_hpbar/
This code implements a health system using ProgressBar in the Godot engine. The Health Bar displays the playerβs current health, while the Damage Bar visually represents delayed damage effects. A Timer is used to add a delay to the Damage Bar update, emphasizing health reduction visually.
Properties
timer (
Timernode): Manages the visual delay for damage effects. When damage occurs, the timer starts, and after a set duration, theDamage Barupdates.damage_bar (
ProgressBarnode): A separateProgressBarfrom theHealth Bar, showing health reduction with a visual delay to reflect damage.health (
int): A variable representing the playerβs current health, which is set through the_set_healthfunction.
Functions
func init_health(_health: int) -> void
- Description: Initializes the playerβs health value.
- Parameters:
_health: Initial health value.- Operation:
- Assigns
_healthto thehealthvariable to initialize the playerβs health.- Sets
max_valueandvalueproperties to display initial health on bothHealth BarandDamage Bar.
func _set_health(new_health: int) -> void
- Description: A setter function that updates the
healthvalue and starts the timer if damage occurs.- Parameters:
new_health: The health value to update.- Operation:
- Sets
new_healthtohealthand updates thevalueproperty.- Deletes the node if
healthdrops to 0 or below.- If health decreases (when
new_healthis lower thanprev_health), the timer triggers a visual delay effect on theDamage Bar.- If health does not decrease,
Damage Barvalueis immediately set to matchhealth.
func _on_timer_timeout() -> void
- Description: A function that executes when the timer ends, updating the
Damage Barto follow theHealth Barvalue.- Operation:
- Sets
damage_bar.valueto matchhealth, aligning both bars.
Code Flow
- The init_health function is called to set the initial health value.
- Whenever the health value changes, the _set_health function is called.
- If health decreases, the timer starts, and _on_timer_timeout is executed after a delay.
- When _on_timer_timeout executes,
Damage Baris set to matchHealth Bar.
Reference Video
πΌ https://youtu.be/f90ieBOoIYQ?si=nyOZOAt1t7tkZuJk
Support
π» Support Link for the Developer
For more development tips and insights, check out my social channels!
π²Link tree
If youβd like to know more about me! π΅οΈπ» About Redping