CodeBlanks Logo Icon CodeBlanks

Custom signal

Learn to define custom signals with the signal keyword.

Defining custom signals

To create custom events for your game logic, you use the signal keyword. Signal declarations are typically placed at the very top of the script, before variables.

extends CharacterBody2D

signal health_changed(new_health)
signal player_died

Custom signals can be declared with or without parameters, just like built-in signals.

The power of parameterized signals

Adding parameters to custom signals conveys rich data to listeners. You can also use type hints to clarify the data types transported by the signal.

signal coin_collected(amount: int)
signal level_completed(level_name: String, score: int)

This allows the listener function to receive the necessary values directly as arguments when triggered.

Practice Define custom signal and print

Define signal oyuna_baslandi at the top. Inside _ready, print "Ozel sinyal tanimlandi" to the console.

Starter code

Player.cs
extends Node

# Sinyali buraya tanimla

func _ready():
    # Konsola yazdir

Sample solution

Player.cs
extends Node

signal oyuna_baslandi

func _ready():
    print("Ozel sinyal tanimlandi")

Expected output

Ozel sinyal tanimlandi

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 How do you declare a custom signal named 'can_degisti' carrying the new health value in GDScript?

  • event can_degisti(yeni_can)
  • signal can_degisti(yeni_can) Correct answer
  • var can_degisti = Signal(yeni_can)
  • def signal can_degisti(yeni_can)

Explanation: In GDScript, custom signals are declared using the 'signal' keyword followed by the name and argument list.

2 Write custom signal

Question
# Skor degistiginde yayinlanacak sinyal
____ score_updated(new_score: int)
Answer
# Skor degistiginde yayinlanacak sinyal
signal score_updated(new_score: int)

Explanation: The 'signal' keyword is used to declare a custom signal.

3 Signal definition elements

Term Meaning
signal Signal declaration keyword
player_died Parameterless custom signal example
score_changed(v) Parameterized custom signal
new_health: int Typed signal parameter

All lessons in Intermediate