37. Godot signals and Callables
signal reward_requested(points: int)
var received_points: int = 0func run() -> int:
if not reward_requested.is_connected(_receive_reward):
reward_requested.connect(_receive_reward)
reward_requested.emit(5)
return received_pointsfunc _receive_reward(points: int) -> void:
received_points += pointsA 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.
func add_points(base: int, bonus: int) -> int:
return base + bonusfunc 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.
