Count a Group
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 14 of 27.
A list knows its length. .size() on a group's list counts its nodes:
var count = get_tree().get_nodes_in_group("coins").size()
print("There are ", count, " coins")A game uses this to know how many coins are left or how many enemies are alive.
Count the group. Never type the number yourself. If you add a coin to the level, the count still matches. The five coins here are in the group coins, set in the scene.
Challenge
EasyPrint how many coins there are to find.
- In
_ready(), giveprint("Coins to find: ")a second value after a comma. - Make it the number of nodes in the group
coins, with.size().
Press Play: the Console shows Coins to find: 5. Then Check.
Try it yourself
extends Node2D
func _ready():
print("Coins to find: ")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Signals and Collisions
Practice on your own: Godot playground