Advertisement
Haze_E1

Godot 4 simple healthbar

Oct 4th, 2023
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D
  2.  
  3. @export var playerHealth : int = 100 #set this in the max value of progress bar
  4. var healthBar : ProgressBar
  5. var healthLabel : Label
  6.  
  7. func _ready():
  8.     healthBar = $HealthBar
  9.     healthBar.value = playerHealth
  10.    
  11.     healthLabel = $HealthBar/HealthLabel
  12.     healthLabel.text = str(playerHealth)
  13.    
  14. #testing it works
  15.  
  16. func _input(event):
  17.     if event is InputEventKey and event.keycode == KEY_E:
  18.         if event.pressed:
  19.             playerHealth -= 10
  20.            
  21.             #stop player health going below 0
  22.             if playerHealth <0:
  23.                 playerHealth = 0
  24.                
  25.             healthBar.value = playerHealth
  26.             healthLabel.text = str(playerHealth)
  27.            
  28.             print ("Took 10 damage. Current Health: " + str(playerHealth))
  29.  
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement