mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-04 14:11:02 +03:00
Merge pull request #1359 from KellyThomas/signals
additional C# signal information
This commit is contained in:
committed by
mhilbrunner
parent
6eaba991b5
commit
0eb9ea52f0
@@ -131,6 +131,7 @@ Below is a list of the most important issues you should be aware of when diving
|
||||
- As explained above, the C# project isn't always kept in sync automatically when things are deleted, renamed or moved in Godot (`#12917 <https://github.com/godotengine/godot/issues/12917>`_)
|
||||
- Writing editor plugins and tool scripts in C# is not yet supported
|
||||
- Exporting a project may not yet work (`#15615 <https://github.com/godotengine/godot/issues/15615>`_)
|
||||
- Signals with parameters are broken in 3.0.2-stable (`#17553 <https://github.com/godotengine/godot/issues/17553>`_)
|
||||
|
||||
Performance of C# in Godot
|
||||
--------------------------
|
||||
|
||||
@@ -51,6 +51,8 @@ This attribute should be used on a `delegate`, whose name signature will be used
|
||||
[Signal]
|
||||
delegate void MySignal(string willSendsAString);
|
||||
|
||||
See also: :ref:`c_sharp_signals`
|
||||
|
||||
Singletons
|
||||
----------
|
||||
|
||||
|
||||
@@ -57,8 +57,10 @@ otherwise it returns true.
|
||||
|
||||
For more advanced type checking, you can look into `Pattern Matching <https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching>`_.
|
||||
|
||||
Signals
|
||||
-------
|
||||
.. _c_sharp_signals:
|
||||
|
||||
C# Signals
|
||||
----------
|
||||
|
||||
For a complete C# example, see the **Handling a signal** section in the step by step :ref:`doc_scripting` tutorial.
|
||||
|
||||
@@ -104,6 +106,38 @@ Emitting signals is done with the ``EmitSignal`` method.
|
||||
|
||||
Notice that you can always reference a signal name with the ``nameof`` keyword (applied on the delegate itself).
|
||||
|
||||
It is possible to bind values when establishing a connection by passing an object array.
|
||||
|
||||
.. code-block:: csharp
|
||||
|
||||
public int Value { get; private set; } = 0;
|
||||
|
||||
private void ModifyValue(int modifier)
|
||||
{
|
||||
Value += modifier;
|
||||
}
|
||||
|
||||
public void SomeFunction()
|
||||
{
|
||||
var plusButton = (Button)GetNode("PlusButton");
|
||||
var minusButton = (Button)GetNode("MunusButton");
|
||||
|
||||
plusButton.Connect("pressed", this, "ModifyValue", new object[] { 1 })
|
||||
minusButton.Connect("pressed", this, "ModifyValue", new object[] { -1 })
|
||||
}
|
||||
|
||||
Signals support parameters and bound values of all the `built-in types <https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/built-in-types-table>`_ and Classes derived from :ref:`Godot.Object <class_Object>`.
|
||||
Consequently any ``Node`` or ``Reference`` will be compatible automatically but custom data objects will need to extend from `Godot.Object` or one of its subclasses.
|
||||
|
||||
.. code-block:: csharp
|
||||
|
||||
public class DataObject : Godot.Object
|
||||
{
|
||||
public string Field1 { get; set; }
|
||||
public string Field2 { get; set; }
|
||||
}
|
||||
|
||||
|
||||
Finally, signals can be created by calling ``AddUserSignal``, but be aware that it should be executed before any use of said signals (with ``Connect`` or ``EmitSignal``).
|
||||
|
||||
.. code-block:: csharp
|
||||
|
||||
Reference in New Issue
Block a user