Player UI Value Not Showing or Updating

Godot Version

4.2.2.stable

Question

Hello all, and obligatory apologies in advance as I’m new to Godot and programming (though have previously coded in JS/Python many moons ago).

I am making a 2D Platformer, mostly by myself though following various tutorials for different parts. The issue I am currently having is that the UI I have created starts blank/empty, and only the experience is updated. I currently have a temporary “hurt” input while I mesh out the rest of the game, and this works to kill the player. However, it does not seem to update the Health, ever.

I am using a global variable (starting to implement as I just learned about Autoloading):

var player_health: int = 3

The update experience/health functions are found in the UI script:

func make_health_update(value) -> void:
	GlobalVariables.player_health -= value
	update_health_label()
	
	if GlobalVariables.death == true:
		death_ui.visible = true
		health_label.visible = false
		experience_label.visible = false


func update_health_label() -> void:
	health_label.text = "Health: " + str(GlobalVariables.player_health)


func make_experience_update(value) -> void:
	experience += value
	update_experience_label()


func update_experience_label() -> void:
	experience_label.text = "Experience: " + str(experience)

And the relevant player script is:

func _process(_delta) -> void:
	if Input.is_action_just_pressed("Hurt"):
		GlobalVariables.player_health -= 1
	if GlobalVariables.player_health <= 0:
		do_player_death()

I have the UI and player in their own scenes, and have the UI instantiated on the player. As I said, the experience label updates correctly (despite starting blank) but the health does not.

Apologies if there isn’t enough information; I will gladly provide more! First time posting on the forums.

Thanks in advance!

It seems that you are updating the variable in the Autoload but not reflecting it on the UI itself. A quick note, variables in gdscript are not automatic recaulculated in real time. I would recommend start with a simple process attribuition

in the UI script

func _process(delta: float) -> void
    $path/to/label.text = str(GlobalVariables.player_health)

to see if it works

This worked smooth as butter! It is not only showing the applicable “3” in the beginning, but is also tracking the health decrease.

Thank you!!!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.