Godot Version
Godot 4
Question
so i am new to godot, and been following youtube tutorials to try it, recently i followed a 4 video tutorial to build a 2d game, now i am trying to add a health bar to the enemies (i am also looking up another tutorial) the problem is the health and damage it taken is defiend in another node called damagable
ill provide the relevent scripts
(damagable.gd) (its a child node of enemy character)
extends Node
class_name Damagable
#@onready var healthbar = $HealthBar
signal on_hit(node : Node, damage_taken : int , knockback_direction : Vector2)
@export var dead_animation_node : String = "death"
@export var health : float = 20 :
get:
return health
set(value):
SignalBus.emit_signal("on_health_changed", get_parent(),value - health)
health = value
func hit(damage : int, knockback_direction : Vector2):
health -= damage
emit_signal("on_hit",get_parent(), damage,knockback_direction)
func _on_animation_tree_animation_finished(anim_name):
if(anim_name == dead_animation_node):
get_parent().queue_free()
…
(script of healthbar.gd) (i added this as child node to enemy)
extends ProgressBar
@onready var timer = $Timer
@onready var damage_bar = $DamageBar
var health = 0 : set = _set_health
func _set_health(new_health):
var prev_health = health
health = min(max_value, new_health)
value = health
if health <= 0:
queue_free()
if health < prev_health:
timer.start()
else:damage_bar.value = health
func _init_health(_health):
health = _health
max_value = health
value = health
damage_bar.max_value = health
damage_bar.value = health
func _on_timer_timeout():
damage_bar.value = health
…
(this the script of enemy/(snail))
extends CharacterBody2D
@onready var healthbar = $HealthBar
@onready var animation_tree : AnimationTree = $AnimationTree
@export var starting_move_direction : Vector2 = Vector2.LEFT
@export var movement_speed : float = 30.0
@export var hit_state : State
@export var health : int
@onready var state_machine : CharacterStateMachine = $CharacterStateMachine
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _ready():
health = 20
animation_tree.active = true
healthbar._init_health(health)
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
var direction : Vector2 = starting_move_direction
if direction && state_machine.check_if_can_move():
velocity.x = direction.x * movement_speed
elif state_machine.current_state != hit_state:
velocity.x = move_toward(velocity.x, 0, movement_speed)
move_and_slide()