How do you use variables in a text label?

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

I am creating a FPS game, for the gui I have a text label that tells the player how much ammo they have left.

func _process(_delta):
if reloading:
	ammo_label.text = ("Reloading...")
	ammo_label.text = (str(current_ammo) + " / " + str(clip_size))

I tried multiple things such as using a placeholder. when ever I run it I get the error that says

Invalid set index ‘text’ (on base: ‘String’) with value of type ‘String’.

I am at a loss of what I am supposed to try next so I figured that someone else might have ran into a similar problem.

:bust_in_silhouette: Reply From: jgodfrey

Assuming your ammo_label variable has a proper reference to your Label node, then this will work:

ammo_label.text = "Reloading"

Though, really, the syntax you show works OK too (but you don’t the need the outer parenthesis in either case). With that in mind, I’d guess that your ammo_label variable doesn’t hold a valid reference to your Label node.

jgodfrey | 2020-03-08 00:05