Menu
CoddyTech

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 = true

The 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 icon

Challenge

Medium

Make E turn on every lamp.

  1. Replace $Lamp1.visible = true with a for loop over get_tree().get_nodes_in_group("lamps").
  2. 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