Format with Percent
Part of the Game Rules section of Coddy's Godot journey. Lesson 8 of 29.
With several numbers, + and str() get long. Write the text once instead, with %d where a whole number goes. Then add % and the number:
"Score: %d" % scoreWith score at 40, this gives Score: 40. For several numbers, put them in a list, in the same order as the %ds:
"Level %d, %d lives" % [level, lives]%s works the same way for any value, text included.
Challenge
EasyShow how many coins there are.
- In
_ready(), aftertotalis set, set$HUD/Score.text. - Use
"Coins: %d of %d"withscoreandtotal.
Press Play: the label shows Coins: 0 of 3. Then Check.
Try it yourself
extends Node2D
var score = 0
var total = 0
func _ready():
total = get_tree().get_nodes_in_group("coins").size()
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