0 votes

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.

Godot version 3.5
in Engine by (15 points)

1 Answer

+1 vote
Best answer

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?

by (286 points)
selected by

I got it going, thank you!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.