CHALLENGE 3: A Custom Signal, Emitted and Connected ==================================================================== TASK ---- Declare a custom signal called coin_collected(amount: int) on one node. Emit it with a sample amount, and connect a function on a different node that prints a message using the amount it received. SCENE STRUCTURE ---------------- Root (Node2D) |-- Coin (Node2D) <- declares and emits the signal |-- Player (Node2D) <- listens for it SOLUTION CODE - coin.gd (attached to Coin) --------------------------------------------- extends Node2D signal coin_collected(amount: int) func _ready() -> void: # In a real game this would fire from a collision, not _ready() - # here it's simulated for the sake of the exercise. coin_collected.emit(10) SOLUTION CODE - root.gd (attached to Root) --------------------------------------------- extends Node2D func _ready() -> void: $Coin.coin_collected.connect(_on_coin_collected) func _on_coin_collected(amount: int) -> void: print("Collected a coin worth ", amount, " points!") OUTPUT ------ Collected a coin worth 10 points! WHY THIS WORKS AS AN ANSWER ---------------------------- signal coin_collected(amount: int) declares a new, typed signal on Coin - the (amount: int) part means anything that connects to this signal will receive one int argument when it fires, the same way a typed function parameter works. The connection has to be made in Root's own _ready(), not Coin's, because Root is the one node that can see both $Coin and its own listener function - Coin itself doesn't need to know Player or Root exist at all, which is exactly the decoupling the chapter's tip box described. coin_collected.emit(10) fires the signal with 10 as the amount. Because root.gd already connected _on_coin_collected to that signal before it fired, Godot automatically calls that function with 10 as its argument the moment .emit() runs - producing the printed message without Coin ever calling _on_coin_collected directly by name.