godot-docs icon indicating copy to clipboard operation
godot-docs copied to clipboard

"signal" documentation confuses me

Open leaf-node opened this issue 8 years ago • 3 comments

https://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_basics.html#signals

It's unclear to me whether the signal your_signal_name code is in the same class as instance.connect("your_signal_name", self, "_callback_no_args"), another class, or whether signal names are global. Does self apply to the signal, or the callback?

I also don't understand the context around signaling. Why should I expect that some other class will call my signal if I don't inform it about that signal? How is this code linked up in the context of an example use case? What is a "regular signal" (and what isn't a "regular signal")? How are signals different from callbacks? Are signals generally or strictly for communicating between instances of the same class?

leaf-node avatar Dec 05 '18 02:12 leaf-node

At the top of the section that I linked to above, it says:

It is often desired to send a notification that something happened in an instance...

However it seems to be the case according to the the following text in that section:

instance.connect("your_signal_name", self, "_callback_no_args")

that signals are triggered externally and have an impact internally, not that the signals impact remote systems unless you include a reference to another instance. The quoted first sentence originally led me to suspect otherwise.

leaf-node avatar Dec 05 '18 02:12 leaf-node

Read it myself, and I think the confusion comes from the fact that there's no distinction made from the "signal" from the "signal handler" which is typically called a "slot". Signals are connected to slots, and instances "emit" signals that are automatically broadcast to connected slots. Here's my go at a code example of using signals and slots:

Object 'X' has signals 'a' and 'b'(var x):

File: X.gd
---------------
signal a
signal b(var x)

Make an instance 'Y' of Object 'X':

File: other.gd
------------------
var y = null
func foo():
  y = X.new()

Other instance 'Z' wants to be notified when 'Y' emits signal 'a':

File: Z.gd
------------------
func a_handler():
  print("a_handler called!")

File: other.gd
-------------------
onready var z = Z.new()

func bar():
  y.connect("a", z, "a_handler")
  y.my_notify()    # This causes z's "a_handler" method to run

File: X.gd
-------------------
func my_notify():
  emit_signal("a")

@sudoman How's this? I think I'll make a PR that changes the current text to be more like the above if it's more helpful.

Dar13 avatar Dec 22 '18 02:12 Dar13

Since #2149 was merged, can this be closed? @sudoman

Dar13 avatar Feb 10 '19 19:02 Dar13