Signals are similar to events in C#. They are a built-in implementation of the Observer Pattern.
You can use the await
keyword to continue execution until the specified signal is fired.
Simply reference the signal after using await
func change_scene(scene: String):
$AnimationPlayer.play("fade_out")
await $AnimationPlayer.animation_finished
get_tree().change_scene_to_file(scene)
This codeblock waits for the AnimationPlayer’s animation_finished signal to fire until continuing execution.
You can pass in additional arguments that the signal emission does not pass, at connection time. You do this using Callable.bind()
.
Say you wanted to connect to the pressed
signal of a bunch of buttons, but all those buttons need to have the same functionality, so it’s redundant to make a bunch of handler methods for each of them.
You could connect all of them to the same handler method, which would work, but say you also needed to know which button was pressed.
In this case, you would need to bind the button reference during connection.
Lets store references to all the buttons using an exported array:
@export var button_array: Array[Button]
pressed
signal of each of the buttons in the array.Button
.
pressed
signal does not contain any arguments by default. We are manually taking in an additional parameter in the signal handler method.