π 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 (
Timer
node): Manages the visual delay for damage effects. When damage occurs, the timer starts, and after a set duration, theDamage Bar
updates.damage_bar (
ProgressBar
node): A separateProgressBar
from 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_health
function.
Functions
func init_health(_health: int) -> void
- Description: Initializes the playerβs health value.
- Parameters:
_health
: Initial health value.- Operation:
- Assigns
_health
to thehealth
variable to initialize the playerβs health.- Sets
max_value
andvalue
properties to display initial health on bothHealth Bar
andDamage Bar
.
func _set_health(new_health: int) -> void
- Description: A setter function that updates the
health
value and starts the timer if damage occurs.- Parameters:
new_health
: The health value to update.- Operation:
- Sets
new_health
tohealth
and updates thevalue
property.- Deletes the node if
health
drops to 0 or below.- If health decreases (when
new_health
is lower thanprev_health
), the timer triggers a visual delay effect on theDamage Bar
.- If health does not decrease,
Damage Bar
value
is immediately set to matchhealth
.
func _on_timer_timeout() -> void
- Description: A function that executes when the timer ends, updating the
Damage Bar
to follow theHealth Bar
value.- Operation:
- Sets
damage_bar.value
to 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 Bar
is 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