up:: [[Godot MOC]]
tags:: #note/develop #on/computing/gamedev
# Signals in Godot
Nodes emit **signals** when some event occurs. This allows you to make nodes communicate without hard-wiring them in code. You can even define new signals specifically for your game.
Signals are Godot's version of the [observer](https://gameprogrammingpatterns.com/observer.html) pattern.
## Connecting A Signal via Code
To connect a signal via code, you need to call the `connect()` method of the signal you want to listen to.
## Custom Signals
To define a custom [[Godot Signals|signal]], use the `signal` keyword along with a name for the new signal:
```gdscript
extends Node2D
signal health_depleted
var health = 10
```
To emit the signal, call `emit()` on the signal:
```gdscript
if health <= 0:
health_depleted.emit()
```
### Signal Arguments
A signal can optionally declare one or more arguments:
```gdscript
extends Node2D
signal health_changed(old_value, new_value)
var health = 10
```
To emit values along with the signal, add them as extra arguments to the `emit()` call:
```gdscript
func take_damage(amount):
var old_health = health
health -= amount
health_changed.emit(old_health, health)
```
## Quick Tips
> [!TIP] Methods of Adding Signals
> Signals can be added through both the editor GUI and through pure code. Using code is necessary when you create nodes or instantiate scenes inside of a script.
> [!TIP] Connecting Signals at Scene Instantiation
> If you want to connect a signal when a scene is instantiated, use the [Node.\_ready()](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-private-method-ready) built-in function. This function is called automatically by the engine when a node is fully instantiated.
## References
Juan Linietsky, Ariel Manzur, and Godot Community. “Godot Engine 4.2 Documentation.” Accessed February 27, 2024. [https://docs.godotengine.org/en/stable/index.html](https://docs.godotengine.org/en/stable/index.html).