Menu
CoddyTech

Lives

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

Many games give you a few lives. Each mistake costs one, and the game ends when they run out. In code, lives are a number:

var lives = 3

In this room, the player sends its hurt signal when it touches the red spikes. It then goes back to the green square. _ready() already shows the lives and connects hurt to _on_player_hurt().

To lose a life, take one away, then show the new number.

challenge icon

Challenge

Easy

In _on_player_hurt():

  1. Take one away from lives.
  2. Update $HUD/Lives, so after one hit it shows Lives: 2.

Try it yourself

extends Node2D

var lives = 3

func _ready():
	$HUD/Lives.text = "Lives: %d" % lives
	$Player.hurt.connect(_on_player_hurt)

func _on_player_hurt():
	pass
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Rules

Practice on your own: Godot playground