Skip to reading
GDScript Programming & Godot

37. Godot signals and Callables

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView illustrations
Code example
signal reward_requested(points: int)
var received_points: int = 0
Code example
func run() -> int:
    if not reward_requested.is_connected(_receive_reward):
        reward_requested.connect(_receive_reward)
    reward_requested.emit(5)
    return received_points
Code example
func _receive_reward(points: int) -> void:
    received_points += points

A signal is declared on an object. connect tells it which Callable to run. emit supplies the arguments. Keep the receiver’s parameter list compatible with the signal. is_connected prevents accidentally wiring the same callback twice.

A Callable can be a named method, Callable(object, method_name), or a lambda. bind adds fixed arguments after the emitted arguments. disconnect removes a connection. CONNECT_ONE_SHOT can make a connection remove itself after one emission.

Code example
func add_points(base: int, bonus: int) -> int:
    return base + bonus
Code example
func run() -> int:
    var add_bonus: Callable = add_points.bind(5)
    return int(add_bonus.call(10))

Figure 11. A typed signal and its receiver are normal GDScript, even inside a PCA record.

Page 46 of 378Permalink
Go to page