Post

πŸ“œ Health Bar System Code Documentation

This document details the Health Bar system coded in GDScript for indie game development.

πŸ“œ Health Bar System Code Documentation

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, the Damage Bar updates.

  • damage_bar (ProgressBar node): A separate ProgressBar from the Health 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 the health variable to initialize the player’s health.
    • Sets max_value and value properties to display initial health on both Health Bar and Damage 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 to health and updates the value property.
    • Deletes the node if health drops to 0 or below.
    • If health decreases (when new_health is lower than prev_health), the timer triggers a visual delay effect on the Damage Bar.
    • If health does not decrease, Damage Bar value is immediately set to match health.


func _on_timer_timeout() -> void

  • Description: A function that executes when the timer ends, updating the Damage Bar to follow the Health Bar value.
  • Operation:
    • Sets damage_bar.value to match health, aligning both bars.

Code Flow

  1. The init_health function is called to set the initial health value.
  2. 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.
  3. When _on_timer_timeout executes, Damage Bar is set to match Health 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

This post is licensed under CC BY 4.0 by the author.