Player Health Code Wont Display Health With Label

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SollyWolly
 export(int) var hp_max: int = 100 
export(int) var hp: int = 100
export(int) var defense: int = 0
onready var healthBar = $CanvasLayer/Label
func get_hp():
	return hp
func set_hp(value):
	if value != hp:
		hp = clamp(value, 0, hp_max)
		$CanvasLayer/Label.text = str(100)
		emit_signal("hp_changed", hp)
#		healthBar.value = hp
		if hp == 0:
func set_hp_max(value):
	if value != hp_max:
		hp_max = max(0, value)
		emit_signal("hp_max_changed", hp_max)
		healthBar.max_value = hp_max
		self.hp = hp

Hey guys, my goal with the code is to display the health and change it when damaged.
But, the label won’t change or even show the health.

Thanks, Solly

It is ridiculously hard for anyone to tell what is going wrong here because the formatting of the code is not correct.
For example, since the fromatting is not showing indentation, we are left to guess how your if statement is structured. Maybe that is where the problem is; who knows?

Below the textbox wherein you type your message is a “Preview”. That shows what your message will look like when posted. You must use that and ensure the code you post is formatted properly.
You can edit your post afterwards as well (click on edit).
Select all your code and click the {} on the toolbar and your code should format properly.

LeslieS | 2023-01-23 05:11

I’m sorry for the misunderstanding I fix the post and the problem with it is the label won’t show the health value

SollyWolly | 2023-01-23 12:15

I also think the $CanvasLayer/Label.text = str(100) is the problem but idk

SollyWolly | 2023-01-23 12:18

Excellent, thanks for fixing the formatting.

I don’t see anything wrong with $CanvasLayer/Label.text = str(100) but since you have a variable for that node you should use it instead and since you are surely wanting to set it to the current hit points:

healthBar.text = hp

In func set_hp_max(value): you have at the end self.hp = hp. Since hp does not change in that function this does not do anything.
Are you trying to fire the set_hp(value) function with this?
That function will not fire unless you specifically call it or make it into a setter for the hp property:

export(int) var hp: int = 100 setget set_hp

The only reason I can see for the label not showing any hp is that the code never got to a point wherein the text property of that label was changed.

LeslieS | 2023-01-23 17:32

I did your suggestion and even then it dosent work, maybe the nodes placement is wrong?

SollyWolly | 2023-01-24 00:48

We are going to need to see your node tree and tell us to which node this code is attached.
Also show the code where you call set_hp(); where your player takes damage and calls that.
You can tell if your label variable is working by putting a print statement in the _ready() function.

func _ready():

If that prints null then the parser is not seeing the label node.

LeslieS | 2023-01-24 01:58

The varible is Label:[Label:1313] and my node tree is follow


SollyWolly | 2023-01-26 00:07

So then the variable is legit and the only thing it can be is it never reaches inside here:

func set_hp(value):
    if value != hp:  

Put a print statement (or a breakpoint) in there and see if it ever gets there.

func set_hp(value):
    if value != hp:  
        print("value = ", value)

If not then show the code where you call the function set_hp().

LeslieS | 2023-01-26 01:47

I never call the function

SollyWolly | 2023-01-31 02:39

It is necessary to call that function if you want to change the text on the label.

LeslieS | 2023-02-02 02:19

:bust_in_silhouette: Reply From: Gluon

Try changing

self.hp = hp


self.text = hp