Menu
CoddyTech

Join a Group

Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 11 of 27.

A game often has many coins or many enemies. A group is a label you put on nodes so you can treat them as one set.

A node joins a group with add_to_group(), usually in _ready():

func _ready():
	add_to_group("coins")

The group name is text in quotes. The first node to join creates the group.

All four coins run coin.gd, so this one line puts all four in the group. The script on Main spins every node in coins:

extends Node2D

# Main spins every node in the group coins.
func _process(delta):
	for coin in get_tree().get_nodes_in_group("coins"):
		coin.rotation += 3 * delta

The for loop comes in two lessons. For now the group is empty, so nothing spins.

challenge icon

Challenge

Easy

Make each coin join a group.

  1. In coin.gd, replace pass in _ready() with a line that adds the coin to the group "coins".
  2. Write the name with a small c.

Press Play: all four coins spin. Then Check.

Try it yourself

extends Area2D

func _ready():
	pass

All lessons in Signals and Collisions

Practice on your own: Godot playground