There are 2 ways to connect to a signal in Godot: UI and Code.
Connect to Godot signal using UI
The UI method looks like this:
You start by navigating to the Node panel. You double click on a signal. You enter values in the input boxes and press “Connect”.
Personally I don’t like this method. After you click Connect you’ll end up opening the text editor anyway. It’s not like you can keep continue using the UI to manage your signals.
This UI method it’s like a mini-wizard. It does try to make things easier for the beginners. But this mix of UI and code obfuscates more than it reveals IMO.
So I would recommend that you always connect signal using code. If you keep this signal management in code it’ll be easier to manage.
Connect to Godot signal using code
How do you connect in code? Easy. I wrote a very detailed article about that – check it out!
Long story short, this is how you connect a signal in code (check the link above for more info):
# game_manager.gd
func on_player_zero_life():
print("Starting game over sequence")
func _ready():
var player_node=get_node("player")
player_node.connect("zero_life", self, "on_player_zero_life")
*
- call
connect
on the node that emits the signal. - pass the node that contains the method.
- pass the name of the method.
Super easy!
Why connecting to signal from UI it’s a bad idea!
Imagine you have a scene with 20 different scenes. Let’s say they are different buildings. When a building explodes, it emits a signal. You want to connect these signals to a game manager node.
Let’s say you start using the UI. Cumbersome, but possible. Later you add some new buildings.
Hmmm. Now you have to also connect these new buildings. Only now you have to double check ALL the buildings in the UI, to make sure you haven’t missed a building.
Not good, eh?
Using code you could do something like:
func _ready():
var buildings=get_node("building_holder")
for building in buildings:
building.connect("destroyed",self,"on_building_destroyed")
3 lines of code will take care of your building signals FOREVER. You can add and remove buildings like a boss.
You can keep track of the methods connecting to signals EASILY. (cause maybe later you create an extra function to connect to “destroyed” signal. You mix them up in UI. Now you have to go through each node and double check it in the Inspector).
The lesson I’d like to impart to you is this: ALWAYS CONNECT SIGNALS USING CODE.