Browse Source

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

Matthew 4 years ago
parent
commit
2a4dcc40d6

+ 22 - 0
tutorials/scripting/c_sharp/c_sharp_differences.rst

@@ -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
 ----------
 

+ 6 - 3
tutorials/scripting/gdscript/gdscript_basics.rst

@@ -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