Topic was automatically imported from the old Question2Answer platform.
Asked By
csmith17979
Old Version
Published before Godot 3 was released.
How would you go about showing text on screen with an integer and string, ie: showing a stats/records screen where it says something like wins: 5 losses: 2?
I was guessing it would be something along the lines of:
extends Label
var x = 5
var y = 2
func _ready():
set_text("wins: " + x + "Losses: " + y)
However, this doesn’t seem to work.
Also, how would I go about adding a line, to get this:
You can do that if you add str() to each of those variables to turn them into strings. You can also use a newline to add a line. The revised line would look like this: set_text("wins: " + str(x) + "\nLosses: " + str(y))
The benefit of using % from the start is that you could easily change the code later so that the output has more structured formatting. For instance, if you later want to give the literals a fixed width of 10 chars, you could use %10d.
Definitely this. This is the entire purpose that format strings serve.