• 2D
  • Problems with health bar

Hello, i apologize if this isn't a proper question, but this is certainly getting on my nerves.

I have the following node setup


    -Root
    ---SingletonA
    ---Main
    -------Level
    -------HUD
    -----------Health(ControlNode)
    -----------HealthBar(Textureprogress)

And in my player script i have this signal: self.connect("healthbar", ROUTETOHEALTH(CONTROLNODE), "_on_health_updated")

I also have this on my player script when the "hurt" routine is fired: emit_signal("healthbar", self.hitpoints) ##self.hitpoints is updated before this part##

On the Health(ControlNode) script i have this:

    extends Control
    onready var HealthBar = $HealthBar
    
    func _on_health_updated(newhealth):
         HealthBar.set_value(newhealth)
         print(HealthBar.value)

According to the printed message, the health value is updated accordingly, yet, graphically, the bar never shrinks or moves, so i am wondering if i'm missing something or if i'm doing a completely wrong approach.

Maybe the min and max ranges on the health bar are so large that nothings appears to be happening? I don't know if that would be causing the issue, but maybe it's something to look at?

min range is set to 0, max range is set to 100, so nope. Funny thing is, when i set value to 20 and set it to update to a higher value when hit (for testing purposes), it does update, but not when i set it to update to a lower value.

Can we see the code updating the value, both examples - to higher and to lower?

Yup, here it is:

extends Control
onready var HealthBar = $HealthBar


func _on_health_updated(newhealth):
     #HealthBar.set_value(newhealth)
     HealthBar.set_value(60)
     print(HealthBar.value)

Then i just set the value to "10" in the editor, when the character gets hit, the bar grows accordingly

But when i do this:

extends Control
onready var HealthBar = $HealthBar


func _on_health_updated(newhealth):
     #HealthBar.set_value(newhealth)
     HealthBar.set_value(2)
     print(HealthBar.value)

Nothing happens

0 and 100, i tried with 1 and 100 but nothing

Are you sure the _on_health_updated signal is being sent when the value is set to 2? Maybe there is a condition or something that is preventing it from being emitted when the health is set to a low value? I've had times where the code that tells the UI to update was causing the issue rather than the UI itself. I have no idea if that is the case here though, but it might be something to look at if you haven't already.

It does, otherwise it wouldn't print the messages confirming the value when the character is hit.

Strangely enough, it works perfectly when set as a child of the player node rather than a child of the HUD node, which kinda grinds my gears a bit....

So, i found the solution, i'm such an idiot, the health bar code was working perfectly......i was just adding it twice as a child, which is why the UI thing that TwistedTwigleg mentioned was happening, the healthbar was updating correctly, one of them to be precise, the other one remained still and gave the corresponding effect.

Anyways, thanks to anyone who participated here!