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 * deltaThe for loop comes in two lessons. For now the group is empty, so nothing spins.
Challenge
EasyIn coin.gd, make each coin join the group coins when the game starts. Press Play, and all four coins spin.
Try it yourself
extends Area2D
func _ready():
pass
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