Every Node in a Group
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 13 of 27.
get_tree().get_nodes_in_group("lamps") gives you every node in the group as a list. Go through the list with for:
for lamp in get_tree().get_nodes_in_group("lamps"):
lamp.visible = trueThe lines under for run once for each node. Each time, lamp is the next node. Five lamps means five turns.
get_tree() is the whole game's tree, so this finds the lamps anywhere in the scene. Add a sixth lamp to the group and this code lights it too.
Challenge
MediumMake E turn on every lamp.
- Replace
$Lamp1.visible = truewith aforloop overget_tree().get_nodes_in_group("lamps"). - Inside the loop, show each lamp.
Press Play and press E: all five lamps turn on. Then Check.
Try it yourself
extends Node2D
func _process(delta):
if Input.is_action_just_pressed("action"):
$Lamp1.visible = true
All lessons in Signals and Collisions
Practice on your own: Godot playground