Godot Version
4.2.1
Question
Hey all, I’m currently making a health bar for the basic enemies in my project and I’m stuck on how I’m supposed to call and use a custom signal via code only
info
I want to make a healthbar scene that can be applied to all basic enemies moving forward.
So far I’ve been attaching a node to the enemy with a script that extends a base class called “damageable” that is called whenever the player hits the enemy. And I’ve made a custom signal that is supposed to emit a signal whenever the enemy is hit.
I have done this like so (I’m not sure if anything of what I have made so far is correct):
signal health_changed(health: int, old_health: int)
func hit(damage : int):
mushroom_health -= damage
health_changed.emit(mushroom_health,mushroom_health + damage)
The enemy healthbar is currently a preloaded scene that is instantiated at enemy runtime. Like so:
func _ready():
var healthbar = preload("res://Resources/healthbar_enemy/healthbar.tscn").instantiate()
add_sibling.call_deferred(healthbar)
And what I want to happen now is for the signal to be connected to the healthbar. I have tried this: (although this doesn’t seem to work)
extends ProgressBar
#self = healthbar
@onready var damagebar = $DamageBar #another progress bar behind the healthbar that indicates the damage taken via another color that disappears after a short delay
@onready var timer = $Timer
# Called when the node enters the scene tree for the first time.
func _ready():
for child in get_parent().get_children():
if child is damageable:
child.health_changed.connect(health_changed)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func health_changed(health, old_health):
timer.start()
self.value = health
damagebar.value = old_health
if !timer.time_left:
damagebar.value = health
I have no real idea what I’m doing and I’m just trying random stuff
Any and all help is appreciated