Wondering about Labels

Godot Version

Godot 4.4

Question

So, I’m trying to make an hp display, and I have a 3 labels, one for the hp, one for the ‘/’ symbol(like, out of) and one for the max hp. My question is, when the text for a label overflows the set boundaries(I don’t know how to explain better, sorry), it adds extra space to the right. So is there a setting to make it so instead of the hp number overflowing into the / symbol, the previous text goes to the left to make space for the new digit?

Is there a reason you can’t make it all one label?

Sounds much messier than one label. You could use one label and combine several variables into one string.

Lets say variable names are current_hp and max_hp - then you could write str(current_hp) + " / " + str(max_hp) to get the line 7 / 25 to appear on your label.

1 Like

Thanks for pointing out I could just use one label, didn’t know I could do that, but there’s still the issue of, for example, if the hp number goes from 5 to 12, the rest of the text is pushed to the right. Any way to make that not happen?

You could put the three labels in an HBoxContainer.

1 Like

You can use str(num).lpad(2 , "0") if your number is in the single digits to have your label pad a 0 to the left of the value of num. Then when your number is in the double digits you can just have it print str(num) and nothing will shift.

I tried using lpad(2 , " ") but I did it in func _process and it was way too fast for me to notice if it worked. But 0 worked.

    if num >= 0 and num < 10:
      text = str(num).lpad(2 , "0") + " / " +  str(100)
    if num >= 10 and num < 100
      text = str(num) + " / " + str(100)```
2 Likes

Hi, if you really want to use the 3 labels. Then you can use a HBoxContainer. Set the horizontal alignment for the hp label to right (the text will go from right to left).

1 label is more preferable. You could have a function that updates the text

var hp: int:
	set(value):
		hp = value
		if is_instance_valid(label):
			update_hp_label(value)
	get:
		return hp


func update_hp_label(value: int):
	label.text = str(value) + "/" + str(max_hp)