CodeBlanks Logo Icon CodeBlanks

emit_signal

Learn to emit a signal with emit_signal.

Emitting signals: emit and emit_signal

To trigger a signal and notify listeners, you emit it. In Godot 4, the most common way is calling signal_name.emit(); alternatively, the object-level emit_signal("signal_name") method can also be used.

signal health_changed(new_health)

func take_damage(amount: int):
    health -= amount
    health_changed.emit(health)  # Triggers the signal

As soon as this line runs, all functions connected to the signal are executed in order.

Emitting signals with parameters

Parameters declared on the signal are passed as arguments to emit() or emit_signal() in sequence.

signal score_added(points, player_id)

func add_score():
    score_added.emit(100, 1)
    # or: emit_signal("score_added", 100, 1)

If the parameter count or types do not match, Godot will generate an error or warning.

Practice Emit and handle signal

Declare signal alert_triggered(msg). In _ready, connect it to _on_alert and call alert_triggered.emit("Tehlike!"). The function should print the message.

Starter code

Player.cs
extends Node

signal alert_triggered(msg)

func _ready():
    # Bagla ve emit et

func _on_alert(msg):
    print(msg)

Sample solution

Player.cs
extends Node

signal alert_triggered(msg)

func _ready():
    alert_triggered.connect(_on_alert)
    alert_triggered.emit("Tehlike!")

func _on_alert(msg):
    print(msg)

Expected output

Tehlike!

Questions and answers

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

1 What is the most common modern syntax to emit a custom signal named 'oyun_bitti' in Godot 4?

  • trigger(oyun_bitti)
  • oyun_bitti.fire()
  • send_signal("oyun_bitti")
  • oyun_bitti.emit() Correct answer

Explanation: In Godot 4, signal objects have a direct 'emit()' method (e.g., oyun_bitti.emit()).

2 Trigger signal

Question
signal level_up

func finish_quest():
    level_up.____()
Answer
signal level_up

func finish_quest():
    level_up.emit()

Explanation: In Godot 4, .emit() is called on the signal object to trigger it.

3 Emitting and triggering terms

Term Meaning
emit() Directly triggers the signal
emit_signal Method to emit signal by name
Argümanlar Data carried to listeners
Handler Function handling the signal

All lessons in Intermediate