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 label so the game runs.
- In
_ready(), turnleftinto text withstr(left). - Keep
"Coins left: "in front of it.
Press Play: the error is gone and the label shows Coins left: 3. Then Check.
Try it yourself
extends Node2D
func _ready():
var left = get_tree().get_nodes_in_group("coins").size()
$HUD/Score.text = "Coins left: " + left
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