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.
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(...).
123
valconnector=healthChanged.connectLambda{current,max->GD.print("Health changed to $current / $max")}
123456
SignalConnectorconnector=SignalConnectors.connectLambda2(healthChanged,Integer.class,Integer.class,(current,max)->GD.print("Health changed to "+current+" / "+max));
123456
valconnector=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.
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.