0 votes

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

Godot version 3.2.1
in Engine by (82 points)
edited by

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

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.

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!

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.

1 Answer

+1 vote
Best answer

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.

by (2,442 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.