Browse Source

Show how to pass a value with a signal (#2886)

I had some trouble finding how to do this in the documentation, but had seen it used in some youtube tutorial. I think it would be fitting to explain the usage on this page.

I do not know the corresponding C# syntax, so I haven't changed that part.
Johnnie Hård 5 years ago
parent
commit
f044f2607e
1 changed files with 50 additions and 0 deletions
  1. 50 0
      getting_started/step_by_step/signals.rst

+ 50 - 0
getting_started/step_by_step/signals.rst

@@ -204,6 +204,56 @@ To emit a signal via code, use the ``emit_signal`` function:
             EmitSignal(nameof(MySignal));
         }
     }
+    
+A signal can also optionally declare one or more arguments. Specify the
+argument names between parentheses:
+
+.. tabs::
+ .. code-tab:: gdscript GDScript
+
+    extends Node
+
+    signal my_signal(value, other_value)
+
+ .. code-tab:: csharp
+
+    public class Main : Node
+    {
+        [Signal]
+        public delegate void MySignal();
+    }
+
+.. note::
+
+    The signal arguments show up in the editor's node dock, and Godot
+    can use them to generate callback functions for you. However, you can still
+    emit any number of arguments when you emit signals. So it's up to you to
+    emit the correct values.
+
+To pass values, add them as the second argument to the ``emit_signal`` function:
+
+.. tabs::
+ .. code-tab:: gdscript GDScript
+
+    extends Node
+
+    signal my_signal(value, other_value)
+
+    func _ready():
+        emit_signal("my_signal", true, 42)
+
+ .. code-tab:: csharp
+
+    public class Main : Node
+    {
+        [Signal]
+        public delegate void MySignal();
+
+        public override void _Ready()
+        {
+            EmitSignal(nameof(MySignal));
+        }
+    }
 
 Conclusion
 ----------