Connecting and using custom signals via code - need help

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 :slight_smile:

Didn’t you say “damagaeble” was a child component of the enemy? But in enemy you’re adding the health bar as a sibling to the enemy, meaning as a child of the enemy’s parent. Then in your health bar you get the parent, which is the parent of the enemy, and check if it’s damageable, which it will not be because damageable would be a child of the enemy.

If I understood correctly. You could’ve made the relationship a bit clearer. There could be any number of reasons why this doesn’t work and there’s no way to know because you’re showing disconnected pieces of code.

Edit: You haven’t given any reason why the health bar should be a child of the enemy’s parent. Why is the health bar simply not a child of the enemy? Then you can get rid of this get_parent().get_children() and have the enemy set up the connection between the health bar and damageable. This way the health bar doesn’t need to know anything about it’s surrounding. You can then put the same health bar wherever you want and it always works as a health bar; the user simply connects the dots and it works.

1 Like

No valid reason, I have used add_sibling in the past and that had worked for me but it makes sense that there’s no real reason for the healthbar not to be a child of the enemy. I will change the code

Edit: It works!
Sort of but I think I can continue on my own from here

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.