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
EasyWhen the game starts, print how many coins are in the group coins, as Coins to find: 5. Count them with .size().
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