mirror of
https://github.com/stan220/godot-docs.git
synced 2026-09-08 18:29:11 +00:00
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.
This commit is contained in:
@@ -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
|
||||
----------
|
||||
|
||||
Reference in New Issue
Block a user