How to connect custom signals across scenes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Broeskoenoe

I’m sorry if this is a very basic question, but I cannot seem to figure this one out.

I’m making a platform game that generates levels as you go along. I have spikes that emit a ‘get_hit’ signal. The game constantly instances these spikes.
I want the game to understand that if the player hits the spikes, the player should die.

Do I connect the ‘get hit’ signal to the player, or the spikes? I tried the former but it didn’t work. I suspect the latter, because that worked for my coin signals. If the latter, how do I run a function in the player scene if the signal is connected to the spikes and not the player? I didn’t experience this last problem with the coins as I could simply run the functions in the coin scene.

:bust_in_silhouette: Reply From: Ev1lbl0w

Signals work like notifications to any object that’s interested in them. So in this case, it’s Player that wants to know if it gets hit, so you should connect the signal to Player, especially if you need to access it.

If you really need the signal code to run on the spike, then you need to somehow get a reference to the Player to access it. You could use autoload/singleton to access them globally, or get_node() if their node path is constant throughout the game.

But the easy solution would be to connect to Player. Can you give more details about what didn’t work? Does the player receive the signal, or it never happens?

I got it going, thank you!

Broeskoenoe | 2022-09-03 13:48