Lesson 08

Collect Coins and Items

Pick up coins and watch a counter climb. Introduces signals: one event, two different files reacting to it.

Loading the game engine. This takes a moment the first time.

Perspective

HOW THE COIN SIGNAL TRAVELS

  1. game_events.gd creates it, with one line: signal coin_collected(value: int).
  2. At startup, hud.gd and this file each call .connect() one time, naming a function to run: .connect(_on_coin_collected).
  3. After that, every time coin.gd calls .emit(value), Godot runs the function each file named. One emit, two functions run.

To trace any signal: highlight its name and press Ctrl + Shift + F.

New code in this lesson

None of the earlier lessons use these. Each one shows the line where it appears here.

print()

Writes to the Output panel at the bottom of the Godot editor. Handy for checking what a value really is.

game.gd
print("All coins collected!")

is_in_group()

True when the node has that group ticked in the Node dock. It is how a coin spots the player.

coin.gd
if body.is_in_group("Player"):

The scenes

■ UI node · ■ 2D node · % unique name · .tscn instanced scene

game.tscn
  • Game Node2D game.gd
  • Player CharacterBody2D player.tscn
  • Coin1 Area2D coin.tscn
  • Coin2 Area2D coin.tscn
  • Coin3 Area2D coin.tscn
  • HUD CanvasLayer hud.tscn
  • Walls Node2D
  • TopWall StaticBody2D hidden block.tscn
  • BottomWall StaticBody2D hidden block.tscn
  • LeftWall StaticBody2D hidden block.tscn
  • RightWall StaticBody2D hidden block.tscn
coin.tscn
  • Coin Area2D coin.gd
  • Polygon2D
  • CollisionShape2D
hud.tscn
  • HUD CanvasLayer hud.gd
  • MarginContainer
  • ScoreLabel Label %
player.tscn
  • Player CharacterBody2D group: Player player.gd
  • Sprite2D
  • CollisionShape2D
block.tscn
  • Block StaticBody2D
  • Polygon2D
  • CollisionPolygon2D

FileSystem

  • game.tscn
  • game.gd
  • coin.tscn
  • coin.gd
  • hud.tscn
  • hud.gd
  • player.tscn
  • player.gd
  • block.tscn

game_events.gd

Switch it on once, under Project > Project Settings, on the Globals tab. Not made yet? Press Create Script to make it right there. Already made it? Press Select Script/Scene, pick it, and press Add. Its Name column should say GameEvents, which Godot fills in for you. That's the name your code uses.

The code

Perspective
extends Node2D

## How many coins are in this level. Change this if you add coins!
@export var total_coins: int = 3

var collected_coins: int = 0

func _ready() -> void:
	# We call ".connect" on the signal "coin_collected", and we put the name
	# "_on_coin_collected" inside it. That is the function Godot will run,
	# written on line 35 below.
	# hud.gd connects to this same signal, but names its own function.
	GameEvents.coin_collected.connect(_on_coin_collected)

# The "_" in "_value" means we ignore it. hud.gd is the file that uses "value".
func _on_coin_collected(_value: int) -> void:
	collected_coins += 1
	# "print()" shows up in Godot's Output panel. The player never sees it.
	if collected_coins >= total_coins:
		print("All coins collected!")