CHALLENGE 3: A Collectible Coin Using Area2D ==================================================================== TASK ---- Build an Area2D "coin" with a CollisionShape2D. Add a CharacterBody2D player tagged into a "player" group. Connect the coin's body_entered signal so that walking into it prints "Coin collected!" and removes the coin from the scene. SETUP ----- 1. Create a "Coin" node as an Area2D, with a CollisionShape2D child (Shape: CircleShape2D, a small radius). 2. Create (or reuse) a "Player" CharacterBody2D with its own CollisionShape2D, and a movement script (e.g. Challenge 1's). 3. Select Player in the Scene panel, open the Node panel's "Groups" tab, and add it to a group named "player". SOLUTION CODE (attached to Coin) ------------------------------------- extends Area2D func _ready() -> void: body_entered.connect(_on_body_entered) func _on_body_entered(body: Node2D) -> void: if body.is_in_group("player"): print("Coin collected!") queue_free() WHY THIS WORKS AS AN ANSWER ---------------------------- This is a direct application of the chapter's own Area2D example. body_entered is a signal Area2D already provides - connecting it in _ready() means _on_body_entered runs automatically any time some other physics body's collision shape overlaps the Coin's own CollisionShape2D, with that other body passed in as the body argument. Checking body.is_in_group("player") before reacting matters because body_entered would fire for ANY physics body that overlaps the coin - not just the player. Tagging Player into the "player" group in the editor (rather than checking, say, body.name == "Player", which would break the moment the node is renamed) is what lets the coin correctly ignore anything else that might wander through it - an enemy, a pushed crate - while still reacting to the actual player. queue_free() removes the Coin node at the end of the current frame (Chapter 3), which is why the coin visibly disappears once collected rather than lingering as an empty, non-functional node in the scene.