#Using a signal to communicate current enemy HP to HUD lifebar

2 messages · Page 1 of 1 (latest)

weary bone
#

I'm trying to connect the EnemyEntity scene to the HUD node by using a signal everytime when the enemy has its hp changed, and i want it to communicate both the HP and Max HP variables, but it seems like i don't understand quite much how i'm supposed to make it work. I'm using a very simple ColorRect node and changing its size depending on the hp and max_hp calculation.

EnemyEntity.gd:



export (int) var max_hp = 1
export (bool) var is_knockbackable = true

var velocity = Vector2.ZERO
var knockback = Vector2.ZERO
var hp = max_hp setget set_hp

signal no_health
signal health_changed


func _ready():
    var main = get_tree().get_nodes_in_group("main").front()
    #print(main)
    connect("health_changed",main,"update_current_enemy_hp",[hp, max_hp])


func _process(delta):
    pass


func set_hp(value):
    hp = value
    emit_signal("health_changed",hp,max_hp)
    if hp <= 0:
        emit_signal("no_health")```

**Main.gd (partial), in which i assigned it to the Main scene which has the "main" group:**
```func update_current_enemy_hp(value, max_value):
    var enemy_bar = $HUD/EnemyHUD/EnemyHPRect
    var hp_calc = float(value/max_value)*100
    
    enemy_bar.rect_size.x = hp_calc
    print(value/max_value)

Doing so, the Godot editor prints me this following error

  <C++ Source>  core/object.cpp:1242 @ emit_signal()
  <Stack Trace> EnemyEntity.gd:27 @ set_hp()
                Enemy_PunkMan.gd:68 @ _on_Hurtbox_area_entered()
still musk
#

When you pass extra arguments when connecting to the signal (connect("health_changed",main,"update_current_enemy_hp",[hp, max_hp]), the [hp, max_hp] part), you "bind" these variables and always pass these as arguments whenever that signal is emitted. In your case, since you have the hp and max_hp variables available when the signal is emitted, you don't need to add that extra information when you connect to the signal.