Menu
CoddyTech

Coin Collector: Collected

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

main.gd holds the game's rules, like how many coins are left. Main has to hear about every coin you take. So each coin sends a signal, and Main listens to all of them.

In coin.gd, declare signal collected and emit it just before the coin frees itself:

if body.is_in_group("player"):
	collected.emit()
	queue_free()

In main.gd, connect every coin in the group, as you did with the spikes:

for coin in get_tree().get_nodes_in_group("coins"):
	coin.collected.connect(_on_coin_collected)

This works in Main's _ready() because Godot readies children before their parent. By then every coin has joined the group.

challenge icon

Challenge

Medium

Two scripts this time:

  1. coin.gd: declare signal collected and emit it before queue_free().
  2. main.gd: in _ready(), connect every coin's collected to a new function _on_coin_collected() that prints Got a coin!.

Try it yourself

extends Node2D

func _ready():
	pass
quiz iconTest yourself

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