Skip to content

Commit ec3e094

Browse files
authored
Merge pull request godotengine#8923 from 31/dev/31/cs-global
Clarify C# GlobalClass exported node behavior
2 parents 050f8f4 + 868d48d commit ec3e094

File tree

7 files changed

+46
-5
lines changed

7 files changed

+46
-5
lines changed

tutorials/scripting/c_sharp/c_sharp_exports.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,18 @@ Since Godot 4.0, nodes can be directly exported without having to use NodePaths.
293293
[Export]
294294
public Node Node { get; set; }
295295
296-
Custom node classes can also be used, see :ref:`doc_c_sharp_global_classes`.
296+
A specific type of node can also be directly exported. The list of nodes shown
297+
after pressing "Assign" in the inspector is filtered to the specified type, and
298+
only a correct node can be assigned.
299+
300+
.. code-block:: csharp
301+
302+
[Export]
303+
public Sprite2D Sprite2D { get; set; }
304+
305+
Custom node classes can also be exported directly. The filtering behavior
306+
depends on whether the custom class is a
307+
:ref:`global class <doc_c_sharp_global_classes>`.
297308

298309
Exporting NodePaths like in Godot 3.x is still possible, in case you need it:
299310

tutorials/scripting/c_sharp/c_sharp_global_classes.rst

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ C# global classes
44
=================
55

66
Global classes (also known as named scripts) are types registered in Godot's editor so they can be used
7-
more conveniently. These classes show up in the *Add Node* and *Create Resource* dialogs,
8-
and :ref:`exported properties <doc_c_sharp_exports>` are restricted to instances of the global class or derived classes.
7+
more conveniently.
8+
9+
- Global classes show up in the *Add Node* and *Create Resource* dialogs.
10+
- If an :ref:`exported property <doc_c_sharp_exports>` is a global class, the
11+
inspector restricts assignment, allowing only instances of that global class
12+
or any derived classes.
13+
914
Global classes are registered with the ``[GlobalClass]`` attribute.
1015

1116
.. code-block:: csharp
@@ -21,8 +26,33 @@ The ``MyNode`` type will be registered as a global class with the same name as t
2126

2227
.. image:: img/globalclasses_addnode.webp
2328

24-
The ``[Icon]`` attribute also allows to provide the path to an icon so it can
25-
be used as the class' icon in the editor.
29+
The *Select a Node* window for the ``MyNode`` exported property filters the list
30+
of nodes in the scene to match the assignment restriction.
31+
32+
.. code-block:: csharp
33+
34+
public partial class Main : Node
35+
{
36+
[Export]
37+
public MyNode MyNode { get; set; }
38+
}
39+
40+
.. image:: img/globalclasses_exportednode.webp
41+
42+
If a custom type isn't registered as a global class, the assignment is
43+
restricted to the Godot type the custom type is based on. For example, inspector
44+
assignments to an export of the type ``MySimpleSprite2D`` are restricted to
45+
``Sprite2D`` and derived types.
46+
47+
.. code-block:: csharp
48+
49+
public partial class MySimpleSprite2D : Sprite2D
50+
{
51+
}
52+
53+
When combined with the ``[GlobalClass]`` attribute, the ``[Icon]`` attribute
54+
allows providing a path to an icon to show when the class is displayed in the
55+
editor.
2656

2757
.. code-block:: csharp
2858
-9 KB
Loading
-12 KB
Loading
4.63 KB
Loading
-11.9 KB
Loading
-9.88 KB
Loading

0 commit comments

Comments
 (0)