ProgressBar not updating

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

I am trying to use a ProgressBar as the health bar for my ship in-game. However, the health bar does not update when my model takes damage. I have tried healthBar.value = health and healthBar.set_value (health), and neither updates the bar.

:bust_in_silhouette: Reply From: jgodfrey

Generally, the code you show looks reasonable. Some things to check

  • Is the code being executed?
  • Does healthBar contain a valid reference to your ProgressBar control?
  • What is the value of health?
  • What are min_value and max_value properties set to on the ProgressBar?
  • Does changing the value property in the inspector change the progress indicator as expected?

1: Yes, I tested and the code is being executed
2: Yes, when the code was run I included print (, which responded correctly
3: The value of health is being correctly updated, I tested with print
4: healthBar.min_value is set in the inspector to 0, and healthBar.max_value is set in the code. I could not test either of these.
5: Yes

JIYProgrammer | 2023-05-17 00:42

Other things to check, what is the step of your progress bar? If it’s not an integer you might have to make sure you cast health to a float. Might also help if you shared some of your code. If percent_visible is set to true, do the numbers update at runtime?

Tom Mertz | 2023-05-17 00:59

healthBar.step is 0.01, and the damage is added as a float.
My code is very divided, would I need to upload all of it? I tried to compartmentalize and make it modular but I only succeeded in adding roots from one script to all others. Also, would you want my scene and prefab trees?
healthBar.percent_visible is true, but nothing updates in the runtime, only from the inspector and editor.

JIYProgrammer | 2023-05-17 01:45

Very strange, really the more info you can give the better we could troubleshooting. Compartmentalized is generally a good thing! I think the code around setting up the progress bar, what signals or methods change it’s value etc would be a good start. Dumb second question, if you just set in your code instead of healthBar.value = health make it healthBar.value = 100.0 or whatever your max value is, does that do anything?

Tom Mertz | 2023-05-17 01:59

Also, if the project itself is available online, sharing it would be an easy way to get additional input. Really, this is likely something quite simple, but it’s difficult to diagnose all of the possibilities via a forum post… :slight_smile:

jgodfrey | 2023-05-17 02:07

I wouldn’t notice because it’s already stuck at 100%. might try setting it to zero, though.

JIYProgrammer | 2023-05-17 02:07

You never said what your health value is. Is it possibly greater than 100?

jgodfrey | 2023-05-17 02:11

How would I share my project?

JIYProgrammer | 2023-05-17 02:17

Yes, it starts greater than 100, but healthBar.max_value is also greater than 100. Also, I don’t see any change when health goes to less than 100.

JIYProgrammer | 2023-05-17 02:19

If it happens to be a public repo in github (for example), you could just share the url. Otherwise, you could zip up the project’s entire folder structure, upload it to a cloud storage service (dropbox, google drive, onedrive, …) and share a link.

jgodfrey | 2023-05-17 02:20

Yes, it starts greater than 100, but healthBar.max_value is also greater than 100.

Ah, I thought max_value was 100 here, but I see that’s not the case. So, your health value isn’t greater than max_value I guess…

jgodfrey | 2023-05-17 02:28

ok, did further testing, apparently the progress bar was only showing when health was under 100. My bad, I thought I’d tested that. I don’t think I have a solution yet, but at least now I know what I need to work on.

JIYProgrammer | 2023-05-17 02:37

Glad you have something to look at. You mentioned that max_value was being set in code. Are you sure that’s working? If not, max_value will default to 100 and any value >= 100 will cause the bar to be completely full - which sounds like what you’re describing.

jgodfrey | 2023-05-17 02:40

:bust_in_silhouette: Reply From: JIYProgrammer

Turned out I was updating the max_value for the wrong ProgressBar. I have changed my code to set healthBar.max_value at the same time I initialize the player.

:bust_in_silhouette: Reply From: Tom Mertz

It might help to pull the healthBar out into a minimal scene to validate, something like this:

extends Control
onready var progress_bar: ProgressBar = $ProgressBar
onready var timer: Timer = $Timer

var start_value: float = 1000.0
var current_value: float

func _ready() -> void:
	# set max_value to 1000.0
	progress_bar.max_value = start_value
	# set value to 10000.0, but max_value restricts this to 1000
	progress_bar.value = 10000.0
	# timer is auto starting, connect it to reduce the progress bar
	timer.connect("timeout", self, "_on_timer_timeout")
func _on_timer_timeout() -> void:
	progress_bar.value -= 5.0

It seems like validating that whatever is setting your max_value is setting it to the right thing (above 100) and then that if you are setting the progress_bar.value to a direct value, instead of subtracting from the existing value, making sure that the health variable is going down or changing to what you expect.

One easy thing to do to validate is play your scene (F6), then go back into Godot and in your scene node area, you’ll see an added tab called “Remote.” This will show you the active values of your game nodes. You can try opening up your progress bar and seeing in realtime what the max_value and value items are in the inspector when you run the game.

Also, feel free to edit your question and add any scripts you think might be relevant for debugging!

Would be interested to see what you find!