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
EasyIn _on_player_picked_up():
- Add 1 to
score. - 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
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