Gradually updating progress bar

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TheJollyReaper

I want my health bars to decrease gradually, think Pokemon style. I’ve tried using timers to decrease the value of the texture bar every set amount of time, I also tried using delta (solution here), but neither are working for me. My game just freezes and becomes unresponsive, or the health bar appears to change instantaneously anyways.

EDIT: Got it working. Here’s what it looks like for anyone interested

Can you give more information? Like what does your code look like?

exuin | 2020-12-29 23:41

I was trying something like this:

My timer function
    func create_timer(duration, body, function, oneshot):
        	timer = Timer.new()
        	timer.set_one_shot(oneshot)
        	timer.set_wait_time(duration)
        	timer.connect("timeout", body, function)
        	add_child(timer)
        	timer.start()

func update_health():
        while health_bar.value > enemy.health:
              create_timer(.25, self, "update_health_bar", true)

func update_health_bar():
        health_bar.value -= 1

Which ended up making my game unresponsive whenever it tried updating the health bar. I was trying to decrease the value of the texture bar by 1 every x amount of seconds until it equaled the current health of the enemy/player.

My other attempt was to have a variable called update_healthbar and have it set to false by default.

func _process(delta):
       if update_healthbar:
              while health_bar.value > enemy.health:
                        health_bar.value -= delta * (some number)
              update_healthbar = false

func update_health():
       update_health_bar = true

In this try, if the number i multiplied by delta was greater than 30, the health bar appeared to update instantly all at once. Anything lower would lead to the game crashing like my first attempt.

TheJollyReaper | 2020-12-30 01:07

The problem with your code is those while loops. A while loop will run until the condition in its header is fulfilled. So the while loop in the first code snippet will create timers until the health bar’s value drops below the specified value instead of making a new timer when the old one runs out. The while loop in the second code snippet will subtract a number from the health bar’s value over and over again until it’s small enough. That’s probably why your game was crashing.

But it looks like you’ve figured out a solution!

exuin | 2020-12-30 01:39

I didn’t realize that the while loop would keep creating timers the entire time. I’m guessing making sure there’s only one timer active at a time would prevent it from crashing?

Regardless a tween does look like a better solution overall, but it’s good to know where my original code had issues.

TheJollyReaper | 2020-12-30 01:52

:bust_in_silhouette: Reply From: Lopy

Animations like that can be done using a Tween. Note that you may need to cancel and restart your Tween if damage is inflicted before the animation is done.

Thanks for pointing me in the right direction, I found exactly how to use a tween to accomplish what I want in the docs.

TheJollyReaper | 2020-12-30 01:13