Needed Explanations on Signals IN DEPTH

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Noob_Maker
:warning: Old Version Published before Godot 3 was released.

I’m working on my project that requires an node to be communicating with it’s child node via signals.
I couldn’t find any sources that explain signals very clearly(how they work, how to emit signals, how to receive signals. etc.). I’m not sure on if I necessarily need to connect the nodes to have said nodes to be communicating(based on what I’ve heard, I believe that you can) and another thing is how to make an “if signal (insert name here) is recieved: do this” statement and a bit extra information on emitting signals. I understand the basics (make a signal and then do this emit_signal() thing to actually send it) but everytime I try this nothing happens and probably what’s going on is that I don’t understand how to do signals in godot.

I would GLADLY appreciate examples but please annotate so that I could get the better picture. I’m having issues with this for over three weeks now and it starting to bug me :confused:

:bust_in_silhouette: Reply From: JTJonny

Guides/Info about signals:
video tutorial: https://youtu.be/l0BkQxF7X3E
http://docs.godotengine.org/en/latest/tutorials/step_by_step/scripting.html#handling-a-signal
http://docs.godotengine.org/en/latest/reference/gdscript.html#signals

You don’t ever have to use signals, but they are nice to have.

There are two kinds of signals

Ones kind are prebuilt into classes. You can find which signal a class has by looking in the docs of that class. Remember classes inherit from other classes so look at those classes too.

how to use:

You have to first connect the signal you want to use to a node with a function you want to trigger/signal.


nodeWithSignal.connect( String signal, Object target, String method)

example: myRigidBody2D.connect( "body_enter", self, "my_function_in_self")

You can use one of the built in functions in that node or create one.

If you want to use a created function, the function has to be able to receive a variable if the signal is sending one. So for this case it might look like this:

func my_function_in_self(body):
	if body == body_im_looking_for:
		print("found it")

The second kind you create yourself, by adding signal my_signal_name at the top of a gdscript. Then when you desire to signal that signal:

emit_signal(" my_signal_name", pass a variable if you want to here)

The emitted signals have to also be connected.

And in order to make the node that you use the function as well or the function is different because the signal is custom?

Noob_Maker | 2017-07-12 22:53

when using signals in gdscript you always need a function to call, which will be defined when you connect it.

( connect() needs to be called from node with signal )

nodeWithSignal.connect( String signal, Object target, String method)

string singnal = Name of signal in string form
object target = Node where function is signal. example: get_node("node2D")
string method = A function in the node you just assigned in string form

JTJonny | 2017-07-12 23:21

Great answer but I’m confused when you said we don’t ever have to use signals. What about button signals, collision signals? I’ve also used custom signals to avoid checking some condition every frame in process functions. Is using signals that way a bad practice?

codevanya | 2017-07-14 17:22

 I've also used custom signals to avoid checking some condition every frame in process functions. 

Yeah if you didn’t use them you would have to check a lot more conditions. From my understanding the built in ones are basically free to use (it won’t add any more load on your game). I’m no programing pro, but I bet most if not all the time its a good idea to use them.

I'm confused when you said we don't ever have to use signals. 

I’m pretty sure you never have to use them, but I could be wrong. (You can do a lot of stuff with those server classes)

JTJonny | 2017-07-14 18:06

( connect() needs to be called from node with signal )

This is not true, just for the record. You can connect from anywhere, including the from the observer by using self as the second argument in the connect() call.

DDoop | 2020-07-20 19:20