Menu
CoddyTech

Update the Label

Part of the Game Rules section of Coddy's Godot journey. Lesson 9 of 29.

A label shows the last text it was given. It does not follow your variables. When score changes, set the label's text again:

func _on_player_picked_up():
	score += 1
	$HUD/Score.text = "Coins: %d of %d" % [score, total]

In this room, the player sends a picked_up signal when it takes a coin. _ready() already connects it to _on_player_picked_up().

challenge icon

Challenge

Easy

In _on_player_picked_up():

  1. Add 1 to score.
  2. Update the label, so after the first coin it shows Coins: 1 of 3.

Try it yourself

extends Node2D

var score = 0
var total = 0

func _ready():
	total = get_tree().get_nodes_in_group("coins").size()
	$HUD/Score.text = "Coins: %d of %d" % [score, total]
	$Player.picked_up.connect(_on_player_picked_up)

func _on_player_picked_up():
	pass
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Rules

Practice on your own: Godot playground