Menu
CoddyTech

Coin Collector: Coins

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

Each of the six coins runs coin.gd, so you write it once. A coin does three things you already know:

  • It joins the group coins, so Main can count the coins later.
  • It listens to its own body_entered.
  • It frees itself when the player touches it.

The player is in the group player, set in the scene. Check for it before freeing the coin, so nothing else picks one up:

func _on_body_entered(body):
	if body.is_in_group("player"):
		queue_free()
challenge icon

Challenge

Easy

Make the coins disappear when the player takes them.

  1. In coin.gd's _ready(), join the group coins and connect body_entered to _on_body_entered.
  2. Write _on_body_entered(body): free the coin if the body is in the group player.

Press Play and walk over the coins: each one disappears. Then Check.

Try it yourself

extends Area2D

func _ready():
	pass

All lessons in Signals and Collisions

Practice on your own: Godot playground