Menu
CoddyTech

Reach the Goal

Part of the Game Rules section of Coddy's Godot journey. Lesson 22 of 29.

A game you can lose needs a way to win. Often it is a place, like a flag at the end of a level.

The flag here is an Area2D called Goal. When a body walks in, it sends body_entered with that body. The player is in the player group, so the goal can tell it apart from other bodies:

func _on_goal_body_entered(body):
	if body.is_in_group("player"):
		$HUD/Message.text = "You win!"

The HUD has an empty Message label across the middle of the screen.

challenge icon

Challenge

Easy

Let the player win by reaching the flag.

  1. In _ready(), connect $Goal.body_entered to _on_goal_body_entered.
  2. In _on_goal_body_entered(body), when the body is in the player group, set $HUD/Message.text to "You win!".

Press Play and walk to the flag: You win! appears across the screen. Then Check.

Try it yourself

extends Node2D

func _ready():
	pass

All lessons in Game Rules

Practice on your own: Godot playground