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

Show how many coins there are.

  1. In _ready(), after total is set, set $HUD/Score.text.
  2. Use "Coins: %d of %d" with score and total.

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

Practice on your own: Godot playground