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
EasyWhen the game starts, make the label show Coins: 0 of 3, using %d twice with score and total.
Try it yourself
extends Node2D
var score = 0
var total = 0
func _ready():
total = get_tree().get_nodes_in_group("coins").size()
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