Menu
CoddyTech

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: " + left

GDScript 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 icon

Challenge

Easy

Fix the label so the game runs.

  1. In _ready(), turn left into text with str(left).
  2. 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

Practice on your own: Godot playground