Add information on onready annotation to C# differences page (#4565)

This commit is contained in:
Matthew
2021-03-19 07:40:22 -04:00
committed by Rémi Verschelde
parent 836f8490f1
commit 2a4dcc40d6
2 changed files with 28 additions and 3 deletions

View File

@@ -128,6 +128,28 @@ This attribute should be used on a `delegate`, whose name signature will be used
See also: :ref:`doc_c_sharp_signals`.
`@onready` annotation
---------------------
GDScript has the ability to defer the initialization of a member variable until the ready function
is called with `@onready` (cf. :ref:`doc_gdscript_onready_annotation`).
For example:
.. code-block:: gdscript
@onready var my_label = get_node("MyLabel")
However C# does not have this ability. To achieve the same effect you need to do this.
.. code-block:: csharp
private Label _myLabel;
public override void _Ready()
{
_myLabel = GetNode<Label>("MyLabel");
}
Singletons
----------

View File

@@ -339,7 +339,8 @@ Here's the list of available annotations:
+------------------------------+---------------------------------------------------------------------------------------------------+
| ``@tool`` | Enable the `Tool mode`_. |
+------------------------------+---------------------------------------------------------------------------------------------------+
| ``@onready`` | Defer initialization of variable until the node is in the tree. See `Onready annotation`_. |
| ``@onready`` | Defer initialization of variable until the node is in the tree. See |
| | :ref:`doc_gdscript_onready_annotation`. |
+------------------------------+---------------------------------------------------------------------------------------------------+
| ``@icon(path)`` | Set the class icon to show in editor. To be used together with the ``class_name`` keyword. |
+------------------------------+---------------------------------------------------------------------------------------------------+
@@ -1726,8 +1727,10 @@ This also means that returning a signal from a function that isn't a coroutine w
type-safety, because a function cannot say that returns an ``int`` but actually give a function state object
during runtime.
Onready annotation
~~~~~~~~~~~~~~~~~~
.. _doc_gdscript_onready_annotation:
`@onready` annotation
~~~~~~~~~~~~~~~~~~~~~
When using nodes, it's common to desire to keep references to parts
of the scene in a variable. As scenes are only warranted to be