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
MediumTwo scripts this time:
coin.gd: declaresignal collectedand emit it beforequeue_free().main.gd: in_ready(), connect every coin'scollectedto a new function_on_coin_collected()that printsGot a coin!.
Try it yourself
extends Node2D
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
6Project: Coin Collector
Coin Collector: WalkCoin Collector: CoinsPractice on your own: Godot playground