Godot can be quite tricky to navigate if you are not used to scene instancing and the nodes based system. So this tutorial will use a very basic and straight forward example on how to allow godot signals to be passed across and between scenes and allow you to get signals from other scenes, which get instantiated in your game. For you to fully understand signals it is important to understand the observer design pattern. In essence one object will send a signal out into the eco system and another one will be listening for that specific signal. Let’s now see how we can setup a basic project to have different scenes intercommunicate with one another.

Want to rather watch video check it out here:

Let’s start off with a new godot project. You can call your project Godot signals across scenes tutorial.

Click into 2d view so we can keep our project simple and create a 2d scene.

Rename the Node2D to Game.

This will now be our main game scene. Save that scene as Game.tscn.

Click on this plus to create two more scenes. Rename their 2d nodes to enemy and player respectively and save those scenes.

In the end you should have this in your FileSystem.

We will now basically use the enemy and player scene and bring them into our game scene then send signals between them. First we need to do some basic setup of our player and our enemy.

Setting up the enemy and player scene for signals to work

To setup our enemy switch over into the enemy scene and right click on the enemy node and add the following nodes. Add the sprite and area2d node. Also add a collisionshape2d under area2d so that your enemy scene tree looks like this.

We want to now add an enemy sprite. I have just used the icon.png which comes with a default godot project.

Now create a Collisionshape2d for your enemy by clicking on CollisionShape2D and creating a rectangleshape2d

Draw the collisionshape2d to be quite large. We want to use this as a detecting area and not as a collider.

Let us now setup our player open up the player scene. Add the following nodes, add a sprite, kinematicbody2d and collider to your scene. You can again use the same icon.png sprite for your sprite node. You should end up with a tree which looks like this.

Draw your rectangleshape2d to be as about as big as the player sprite.

Now go back to your Game.tscn and drag and drop both the player and enemy scene under your Game node.

Move your player and enemy around so that they intersect.

Godot signals in code, connect the scenes

So to start connecting up our scenes we need to add two scripts. Let’s first just start with the enemy script. Right click in the file system and create a new script called Enemy.gd.

Go to the enemy scene and attach it to your enemy root node.

Now let’s start off with the most basic godot signals. This godot signal comes part of the standard signals on the nodes we have. So click on Area2d in the enemy scene and go over to node on the right.

Now double click on body_entered, as we want a signal to happen when a our players kinematicbody enters the area where the enemy is.

Then select the Enemy node and click on connect.

You should now have this code open up.

For now to demonstrate how this works just print hello world in that function.

func _on_Area2D_body_entered(body): print("Hello world")

If you run the project you will now get this message in your output window.

So this is the most basic form of a godot signal. In the next section we will use godot signals in code. Then we will be able to signal to the player that the enemy has seen them. This will then work across and between scenes.

Godot signals connecting multiple scenes

So now to get our player detecting that our enemy has seen them in their view area. We need to add a notifier. As in the observable design pattern a notifier will be our emit_signal method. So to create a signal we first need to register it in the godot system. To do this we need to add the following code in our Enemy.gd script.

signal playerspotted

Your full code for the enemy.gd script will look like this.

extends Node signal playerspotted func _ready(): pass # Replace with function body. func _on_Area2D_body_entered(body): emit_signal("playerspotted") print("I see the player")

So when the player comes into the view area of the enemy the enemy will emit a signal that the player is spotted. Now with this signal you could have multiple scenes listen for this in your game. So one behavior could be notifying other enemies in the game that your player was seen and then they can go attack the player. The other is to notify the player that they were scene and maybe have emergency or warnings go off or a change of music. For our example we will keep it simply we will just have our player print something to the output when they have been seen.

To connect this up we want to go ahead and now create a player script. So right click in file system and create a new gscript called player.gd.

Open up your player.tscn and attach this script to your player root node like this.

Now you can open up the player.gd script and copy and paste this code in there.

extends Node func _ready(): var enemynode = get_tree().get_root().find_node("Enemy",true,false) enemynode.connect("playerspotted",self,"handleplayerspotted") func handleplayerspotted(): print("Enemy has spotted me")

So we go ahead and we find the enemynode using find_node function. Then we simply connect the enemynode to the playerspotted signal and define a method which will handle the signal. If you run the game now you should get this in the output.

So we have now created a bidirectional signal in godot. One where the player can be detected by the enemy using the built in signals and the player also knows that the enemy has seen them. Before we close off this tutorial let’s add a godot signal with custom parameters.

Godot signal with custom parameters

For this we just want to send back a custom parameter through our signal which our player can use. So to do this we need to define our godot signal a little differently.

So when you call emit_signal you can pass in multiple parameters after the signal name like this.

emit_signal("playerspotted","player in my area","player is the godot player")

Then in your player gdscript you can give the input variables of your signal handler function names like this and print them out.

func handleplayerspotted(message,whoistheplayer): print("Enemy has spotted me") print(message) print(whoistheplayer)

If you run this your output should look something like this.

The common use for parameters in godot signals is to pass around current health, damage etc. You can also use this to effect a godot state machine in your game for managing quests. Another common practice is to create an intermediate event/signal handler. Which you can connect to in your scene and emit signals to. This then introduces the bridge or proxy design pattern into your game which allows this intermediate node or scene to allow for the communication between your nodes or scenes.

So that is it for this tutorial. I hope with this simplified example godot signals will make more sense in general. If this explanation has not been clear another way to think of signals is in terms of events. So the scene or node emitting the signal sends or invokes an event and the connected scene or node listens for events. Perhaps the confusion for most people come with the terminology of connecting a signal. Where is most programming practice this is the equivalent to adding an event listener or binding an event listener to an object.

I will now be ending off this godot signals tutorial with a frequently asked questions section and then will go on to a few final words.

Frequently asked questions about godot signals

What is a godot signal? A godot signal can be thought of as an event handler. Which allows you to add a listening object and a object which can send an event when it is triggered. The event handler will listen for events and catch them and do things with them when received from the emitting object.

How can I add godot signals between scenes? To connect godot signals between and across scenes you need to connect them by code. In one scene you will define the signal, this scene or node will be the one triggering the signal/event. Then in the other scene you can connect that node or scene.

How can I create godot custom signals which allow parameters? The emit_signal method allows you to add as many parameters as you like after the signal name. You can simply pass them in and then add the variables in your handler function. Then just use them as needed. Check out this source article for a full example.

How do I connect signal to a function in godot? You can simply do this by using the connect function. So something like this self.connect(“ “,self,”myfunctionname”)

Final words

If you liked this godot signals tutorial. Why not support me by subscribing to my youtube channel here: YouTube

If you are interested in learning game development you can support me by signing up for my unity course where I teach you how to build a unity 3d city builder game from start to finish here: https://skl.sh/2YhzEfe

If you are interested in more of my tutorials check out some of these: