Skip to content

Commit cfa455e

Browse files
committed
c_sharp_exports.rst: add complex getter/setter note
1 parent 0cb17c0 commit cfa455e

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

tutorials/scripting/c_sharp/c_sharp_exports.rst

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,42 @@ Properties with a backing field use the default value of the backing field.
7373

7474
.. code-block:: csharp
7575
76-
private string _greeting = "Hello World";
76+
private int _number = 2;
7777
7878
[Export]
79-
public string GreetingWithBackingField
79+
public int NumberWithBackingField
8080
{
81-
get => _greeting;
82-
set => _greeting = value;
81+
get => _number;
82+
set => _number = value;
8383
}
8484
85+
.. note::
86+
87+
A property's ``get`` is not actually executed to determine the default
88+
value. Instead, Godot analyzes the C# source code. This works fine for most
89+
cases, such as the examples on this page. However, some properties are too
90+
complex for the analyzer to understand.
91+
92+
For example, the following property attempts to use math to display the
93+
default value as ``5`` in the property editor, but it doesn't work:
94+
95+
.. code-block:: csharp
96+
97+
[Export]
98+
public int NumberWithBackingField
99+
{
100+
get => _number + 3;
101+
set => _number = value - 3;
102+
}
103+
104+
private int _number = 2;
105+
106+
The analyzer doesn't understand this code and falls back to the default
107+
value for ``int``, ``0``. However, when running the scene or inspecting a
108+
node with an attached tool script, ``_number`` will be ``2``, and
109+
``NumberWithBackingField`` will return ``5``. This difference may cause
110+
confusing behavior. To avoid this, don't use complex properties.
111+
85112
Any type of ``Resource`` or ``Node`` can be exported. The property editor shows
86113
a user-friendly assignment dialog for these types. This can be used instead of
87114
``GD.Load`` and ``GetNode``. See :ref:`Nodes and Resources <doc_c_sharp_exports_nodes>`.

0 commit comments

Comments
 (0)