Skip to content

Signals and callables

Connect signals to react to events and pass callables to Godot APIs.

Connect a built-in signal

The following Player example creates a Timer in _ready() and connects its timeout signal to a lambda. The callback calls heal(1) every second.

1
2
3
4
5
6
7
8
9
import godot.api.Timer
import godot.extension.connectLambda

override fun _ready() {
    val timer = Timer()
    addChild(timer)
    timer.timeout.connectLambda { heal(1) }
    timer.start(1.0)
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import godot.api.Timer;
import godot.extension.SignalConnectors;

@Override
public void _ready() {
    Timer timer = new Timer();
    addChild(timer);
    SignalConnectors.connectLambda0(timer.getTimeout(), () -> heal(1));
    timer.start(1.0);
}
1
2
3
4
5
6
7
8
9
import godot.api.Timer
import godot.extension.SignalConnectors

override def _ready(): Unit = {
  val timer = new Timer()
  addChild(timer)
  SignalConnectors.connectLambda0(timer.getTimeout(), () => heal(1))
  timer.start(1.0)
}

Declare a signal

The signal is registered without @Emit. This optional annotation names the parameters shown in Godot; here they are current and max. Import Emit from godot.annotation and signal2 (Kotlin) or Signal2 (Java and Scala) from godot.core.

1
2
@Emit("current", "max")
val healthChanged by signal2<Int, Int>()
1
2
3
@Emit(parameters = {"current", "max"})
public final Signal2<Integer, Integer> healthChanged =
    Signal2.create(this, "healthChanged");
1
2
3
@Emit(parameters = Array("current", "max"))
val healthChanged: Signal2[Integer, Integer] =
  Signal2.create(this, "healthChanged")

The Kotlin delegate derives the owner and member name. In Java and Scala, pass the owner and exact member name to Signal2.create. Godot-JVM converts healthChanged to health_changed for Godot.

Signal2 accepts two arguments with the declared types. Typed signals catch mismatched argument counts and types at compile time.

Emit a signal

This emits the current and maximum health:

1
healthChanged.emit(health, 100)
1
healthChanged.emit(health, 100);
1
healthChanged.emit(health, 100)

Connect your signal

The following connections are made in _ready().

Connect a lambda

Import SignalConnector from godot.extension when naming the return type. For inline subscriptions, Kotlin provides connectLambda, and Java and Scala use SignalConnectors.connectLambdaN(...).

1
2
3
val connector = healthChanged.connectLambda { current, max ->
    GD.print("Health changed to $current / $max")
}
1
2
3
4
5
6
SignalConnector connector = SignalConnectors.connectLambda2(
    healthChanged,
    Integer.class,
    Integer.class,
    (current, max) -> GD.print("Health changed to " + current + " / " + max)
);
1
2
3
4
5
6
val connector = SignalConnectors.connectLambda2(
  healthChanged,
  classOf[Integer],
  classOf[Integer],
  (current: Integer, max: Integer) => GD.print(s"Health changed to $current / $max")
)

Keep the returned connector to check or disconnect the subscription. Store it in a field when its lifetime spans several methods. To end the subscription:

1
connector.disconnect()
1
connector.disconnect();
1
connector.disconnect()

Connect a registered method

The following Player handler connects in _ready(). The target is the current scene instance, this. Kotlin uses connectMethod from godot.extension; Java and Scala use MethodStringName2 from godot.core.

1
2
3
4
5
6
7
8
@Register
fun onHealthChanged(current: Int, max: Int) {
    GD.print("Health: $current / $max")
}

override fun _ready() {
    healthChanged.connectMethod(this, Player::onHealthChanged)
}

MethodStringNameN takes the Godot name

SignalN.create(...) converts to snake_case; MethodStringNameN(...) does not. In Java and Scala, pass on_health_changed, the name Godot knows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@Register
public void onHealthChanged(int current, int max) {
    GD.print("Health: " + current + " / " + max);
}

private final MethodStringName2<Player, Void, Integer, Integer> onHealthChangedName =
    new MethodStringName2<>("on_health_changed");

@Override
public void _ready() {
    SignalConnectors.connectMethod2(
        healthChanged,
        this,
        onHealthChangedName
    );
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@Register
def onHealthChanged(current: Int, max: Int): Unit = {
  GD.print(s"Health: $current / $max")
}

private val onHealthChangedName =
  new MethodStringName2[Player, Void, Integer, Integer]("on_health_changed")

override def _ready(): Unit = {
  SignalConnectors.connectMethod2(
    healthChanged,
    this,
    onHealthChangedName
  )
}

Kotlin's connectMethod takes a method reference. Java and Scala use SignalConnectors.connectMethod2 with a typed method name. Declare that name once for a script method. Generated Godot base-class methods already provide ...Name fields, such as Node.queueFreeName.

Pass a callable to Godot

The following recoverLater() method schedules a registered method with a Tween.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import godot.core.methodCallable0

@Register
fun announceRecovery() {
    GD.print("Recovery complete")
}

fun recoverLater() {
    val tween = createTween()
    tween.tweenInterval(1.0)
    tween.tweenCallback(methodCallable0(this, Player::announceRecovery))
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import godot.core.MethodCallable0;
import godot.core.MethodStringName0;

@Register
public void announceRecovery() {
    GD.print("Recovery complete");
}

private final MethodStringName0<Player, Void> announceRecoveryName =
    new MethodStringName0<>("announce_recovery");

public void recoverLater() {
    var tween = createTween();
    tween.tweenInterval(1.0);
    tween.tweenCallback(MethodCallable0.create(
        this, announceRecoveryName
    ));
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import godot.core.{MethodCallable0, MethodStringName0}

@Register
def announceRecovery(): Unit = {
  GD.print("Recovery complete")
}

private val announceRecoveryName =
  new MethodStringName0[Player, Void]("announce_recovery")

def recoverLater(): Unit = {
  val tween = createTween()
  tween.tweenInterval(1.0)
  tween.tweenCallback(MethodCallable0.create(
    this, announceRecoveryName
  ))
}

Scala signal types

Use boxed Java types such as Integer and java.lang.Boolean as signal type arguments. Primitive Scala type arguments may register as untyped Variants.