Numbers into Text
Part of the Game Rules section of Coddy's Godot journey. Lesson 7 of 29.
A label's text is a string, and a score is a number. This line stops the game with an error:
$HUD/Score.text = "Coins left: " + leftGDScript does not add a number to a string. str() turns a value into text, then + joins the two strings:
str(3) # "3"
"Lives: " + str(3) # "Lives: 3"Mind the quotes. str(left) is the number in the variable. "left" is the word left.
Challenge
EasyFix the error so the label shows Coins left: 3. Turn left into text with str().
Try it yourself
extends Node2D
func _ready():
var left = get_tree().get_nodes_in_group("coins").size()
$HUD/Score.text = "Coins left: " + left
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Rules
2Score on Screen
A Label on ScreenNumbers into TextFormat with PercentUpdate the LabelA Clock on ScreenPractice on your own: Godot playground