Skip to content

Commit 27e7f84

Browse files
Add page about using global classes in C# (godotengine#6740)
Co-authored-by: Max Hilbrunner <[email protected]>
1 parent 6fdf4c8 commit 27e7f84

File tree

7 files changed

+57
-1
lines changed

7 files changed

+57
-1
lines changed

tutorials/scripting/c_sharp/c_sharp_exports.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ Since Godot 4.0, nodes can be directly exported without having to use NodePaths.
263263
[Export]
264264
public Node Node { get; set; }
265265
266+
Custom node classes can also be used, see :ref:`doc_c_sharp_global_classes`.
267+
266268
Exporting NodePaths like in Godot 3.x is still possible, in case you need it:
267269

268270
.. code-block:: csharp
@@ -296,7 +298,8 @@ Therefore, if you specify a type derived from Resource such as:
296298
private AnimationNode Resource;
297299
298300
The drop-down menu will be limited to AnimationNode and all
299-
its inherited classes.
301+
its inherited classes. Custom resource classes can also be used,
302+
see :ref:`doc_c_sharp_global_classes`.
300303

301304
It must be noted that even if the script is not being run while in the
302305
editor, the exported properties are still editable. This can be used
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.. _doc_c_sharp_global_classes:
2+
3+
C# global classes
4+
=================
5+
6+
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.
9+
Global classes are registered with the ``[GlobalClass]`` attribute.
10+
11+
.. code-block:: csharp
12+
13+
using Godot;
14+
15+
[GlobalClass]
16+
public partial class MyNode : Node
17+
{
18+
}
19+
20+
The ``MyNode`` type will be registered as a global class with the same name as the type's name.
21+
22+
.. image:: img/globalclasses_addnode.webp
23+
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.
26+
27+
.. code-block:: csharp
28+
29+
using Godot;
30+
31+
[GlobalClass, Icon("res://Stats/StatsIcon.svg")]
32+
public partial class Stats : Resource
33+
{
34+
[Export]
35+
public int Strength { get; set; }
36+
37+
[Export]
38+
public int Defense { get; set; }
39+
40+
[Export]
41+
public int Speed { get; set; }
42+
}
43+
44+
.. image:: img/globalclasses_createresource.webp
45+
46+
The ``Stats`` class is a custom resource registered as a global class. :ref:`Exporting properties <doc_c_sharp_exports>` of the
47+
type ``Stats`` will only allow instances of this resource type to be assigned, and the inspector
48+
will let you create and load instances of this type easily.
49+
50+
.. image:: img/globalclasses_exportedproperty1.webp
51+
52+
.. image:: img/globalclasses_exportedproperty2.webp
17.7 KB
Loading
19.9 KB
Loading
16.7 KB
Loading
14.1 KB
Loading

tutorials/scripting/c_sharp/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ C#
1212
c_sharp_variant
1313
c_sharp_signals
1414
c_sharp_exports
15+
c_sharp_global_classes
1516
c_sharp_style_guide

0 commit comments

Comments
 (0)