Menu
CoddyTech

Your Own Signal

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

You can give a node your own signals. Declare one at the top of the script with signal:

extends Area2D

signal hit

Send it with emit() when the thing happens:

func _on_body_entered(body):
	hit.emit()

Older code may use emit_signal("hit"). It does the same.

The spikes only report that someone got hit. What a hit does is up to the code that listens. Here the script on Main listens for hit:

extends Node2D

# Main listens for the spikes' hit signal, once the spikes have one.
func _ready():
	if $Spikes.has_signal("hit"):
		$Spikes.hit.connect(_on_spikes_hit)

func _on_spikes_hit():
	print("Ouch!")
	$Player.modulate = Color(1, 0.42, 0.42)

You will write listeners like this soon. In this lesson you edit only the spikes.

challenge icon

Challenge

Easy

Give the spikes a signal and send it when someone steps on them:

  1. Declare signal hit at the top of spikes.gd.
  2. In _on_body_entered, send it with hit.emit().

Walk onto the spikes. The Console should show Ouch! and you turn coral.

Try it yourself

extends Area2D

func _ready():
	body_entered.connect(_on_body_entered)

func _on_body_entered(body):
	pass
quiz iconTest yourself

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

All lessons in Signals and Collisions

Practice on your own: Godot playground