Menu
CoddyTech

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" % score

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

Challenge

Easy

When 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()
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