Emitting and receiving signals in between scenes

Godot Version



Good day to everyone! I just want to ask some help about this problem I encountered with.

Basically, this is the structure of my game: the main_scene handles the scene transitions, and there’s a scene_manager that holds all the available scenes. When the scene emits a signal to the scene_manager that “Hey, I need a scene, here’s the name of the scene.”.



Now, please tell me if there’s a more efficient way to handle this.

Here’s a room named od_room2 and there are Area2Ds (Doors) that waits for when a body of type Player enters.

How can I make it so that when the player enters the “Doors”, say like, "DoorToElf, it emits a signal that will be received by the root node of the scene (“Room2”). Then the Room2 scene, (assuming it was instanced in the main_scene, for this problem, let’s just say it is) will send a signal to the main_scene that says “Hey, here’s the name of the scene”, then the main_scene will pass it to the scene_manager, then the scene will be loaded.

Side note: My initial plan was to make the main_scene a “listener” for all the signals from the current scene it plays.

Yes so, when room 2 received the signal of player entered the door, create a signal in that script you want to connect with main scene, so on ready function, do like this:

your_signal.connect(get_parent().your_function) #or get_tree().current_scene
# when player entered the door emit, your signal

If you are spawning the room by codes of main scene, then you can simply connect them, like this:

new_room.your_signal.connect(your_function) # after add_child
1 Like

Not sure if it 100% solves your problem, but I like using an autoload for managing game-wide signals where it matters more that “a thing happened somewhere in the game” than that “a specific thing did something.” You can still get some unique functionality per-object by passing arguments with the signal.

## in one script...
func my_function():
## in another script...
func _ready():

Makes things super easy as long as you’re careful.

1 Like

Hello! This definitely solved my problem! Thank you so much!

Here’s what I did:

I used an autoloaded game wide signal messenger, the message bus pattern thingy, right! It made me understand how to use the signals in between scenes!

I also made a boolean to keep track of the state of the connection so I can disconnect it once its job is done.

Here’s what my main_scene looks like now:

Again, I can’t thank you enough! Thank you so much!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.