diff --git a/tutorials/scripting/c_sharp/c_sharp_exports.rst b/tutorials/scripting/c_sharp/c_sharp_exports.rst index dbf79e12dbd..0e699d55b2d 100644 --- a/tutorials/scripting/c_sharp/c_sharp_exports.rst +++ b/tutorials/scripting/c_sharp/c_sharp_exports.rst @@ -263,6 +263,8 @@ Since Godot 4.0, nodes can be directly exported without having to use NodePaths. [Export] public Node Node { get; set; } +Custom node classes can also be used, see :ref:`doc_c_sharp_global_classes`. + Exporting NodePaths like in Godot 3.x is still possible, in case you need it: .. code-block:: csharp @@ -296,7 +298,8 @@ Therefore, if you specify a type derived from Resource such as: private AnimationNode Resource; The drop-down menu will be limited to AnimationNode and all -its inherited classes. +its inherited classes. Custom resource classes can also be used, +see :ref:`doc_c_sharp_global_classes`. It must be noted that even if the script is not being run while in the editor, the exported properties are still editable. This can be used diff --git a/tutorials/scripting/c_sharp/c_sharp_global_classes.rst b/tutorials/scripting/c_sharp/c_sharp_global_classes.rst new file mode 100644 index 00000000000..d595fd94784 --- /dev/null +++ b/tutorials/scripting/c_sharp/c_sharp_global_classes.rst @@ -0,0 +1,52 @@ +.. _doc_c_sharp_global_classes: + +C# global classes +================= + +Global classes (also known as named scripts) are types registered in Godot's editor so they can be used +more conveniently. These classes show up in the *Add Node* and *Create Resource* dialogs, +and :ref:`exported properties ` are restricted to instances of the global class or derived classes. +Global classes are registered with the ``[GlobalClass]`` attribute. + +.. code-block:: csharp + + using Godot; + + [GlobalClass] + public partial class MyNode : Node + { + } + +The ``MyNode`` type will be registered as a global class with the same name as the type's name. + +.. image:: img/globalclasses_addnode.webp + +The ``[Icon]`` attribute also allows to provide the path to an icon so it can +be used as the class' icon in the editor. + +.. code-block:: csharp + + using Godot; + + [GlobalClass, Icon("res://Stats/StatsIcon.svg")] + public partial class Stats : Resource + { + [Export] + public int Strength { get; set; } + + [Export] + public int Defense { get; set; } + + [Export] + public int Speed { get; set; } + } + +.. image:: img/globalclasses_createresource.webp + +The ``Stats`` class is a custom resource registered as a global class. :ref:`Exporting properties ` of the +type ``Stats`` will only allow instances of this resource type to be assigned, and the inspector +will let you create and load instances of this type easily. + +.. image:: img/globalclasses_exportedproperty1.webp + +.. image:: img/globalclasses_exportedproperty2.webp diff --git a/tutorials/scripting/c_sharp/img/globalclasses_addnode.webp b/tutorials/scripting/c_sharp/img/globalclasses_addnode.webp new file mode 100644 index 00000000000..8e8378dc271 Binary files /dev/null and b/tutorials/scripting/c_sharp/img/globalclasses_addnode.webp differ diff --git a/tutorials/scripting/c_sharp/img/globalclasses_createresource.webp b/tutorials/scripting/c_sharp/img/globalclasses_createresource.webp new file mode 100644 index 00000000000..80de8c1111d Binary files /dev/null and b/tutorials/scripting/c_sharp/img/globalclasses_createresource.webp differ diff --git a/tutorials/scripting/c_sharp/img/globalclasses_exportedproperty1.webp b/tutorials/scripting/c_sharp/img/globalclasses_exportedproperty1.webp new file mode 100644 index 00000000000..a9cca77ae03 Binary files /dev/null and b/tutorials/scripting/c_sharp/img/globalclasses_exportedproperty1.webp differ diff --git a/tutorials/scripting/c_sharp/img/globalclasses_exportedproperty2.webp b/tutorials/scripting/c_sharp/img/globalclasses_exportedproperty2.webp new file mode 100644 index 00000000000..7fcab655197 Binary files /dev/null and b/tutorials/scripting/c_sharp/img/globalclasses_exportedproperty2.webp differ diff --git a/tutorials/scripting/c_sharp/index.rst b/tutorials/scripting/c_sharp/index.rst index 821c7e50bb3..f08d55fbe37 100644 --- a/tutorials/scripting/c_sharp/index.rst +++ b/tutorials/scripting/c_sharp/index.rst @@ -12,4 +12,5 @@ C# c_sharp_variant c_sharp_signals c_sharp_exports + c_sharp_global_classes c_sharp_style_guide