c_sharp_global_classes.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. .. _doc_c_sharp_global_classes:
  2. C# global classes
  3. =================
  4. Global classes (also known as named scripts) are types registered in Godot's
  5. editor so they can be used more conveniently.
  6. :ref:`In GDScript <doc_gdscript_basics_class_name>`, this is achieved
  7. using the ``class_name`` keyword at the top of a script. This page describes how
  8. to achieve the same effect in C#.
  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. Global classes are registered with the ``[GlobalClass]`` attribute.
  14. .. code-block:: csharp
  15. using Godot;
  16. [GlobalClass]
  17. public partial class MyNode : Node
  18. {
  19. }
  20. .. warning::
  21. The file name must match the class name in **case-sensitive** fashion.
  22. For example, a global class named "MyNode" must have a file name of
  23. ``MyNode.cs``, not ``myNode.cs``.
  24. The ``MyNode`` type will be registered as a global class with the same name as the type's name.
  25. .. image:: img/globalclasses_addnode.webp
  26. The *Select a Node* window for the ``MyNode`` exported property filters the list
  27. of nodes in the scene to match the assignment restriction.
  28. .. code-block:: csharp
  29. public partial class Main : Node
  30. {
  31. [Export]
  32. public MyNode MyNode { get; set; }
  33. }
  34. .. image:: img/globalclasses_exportednode.webp
  35. If a custom type isn't registered as a global class, the assignment is
  36. restricted to the Godot type the custom type is based on. For example, inspector
  37. assignments to an export of the type ``MySimpleSprite2D`` are restricted to
  38. ``Sprite2D`` and derived types.
  39. .. code-block:: csharp
  40. public partial class MySimpleSprite2D : Sprite2D
  41. {
  42. }
  43. When combined with the ``[GlobalClass]`` attribute, the ``[Icon]`` attribute
  44. allows providing a path to an icon to show when the class is displayed in the
  45. editor.
  46. .. code-block:: csharp
  47. using Godot;
  48. [GlobalClass, Icon("res://Stats/StatsIcon.svg")]
  49. public partial class Stats : Resource
  50. {
  51. [Export]
  52. public int Strength { get; set; }
  53. [Export]
  54. public int Defense { get; set; }
  55. [Export]
  56. public int Speed { get; set; }
  57. }
  58. .. image:: img/globalclasses_createresource.webp
  59. The ``Stats`` class is a custom resource registered as a global class. :ref:`Exporting properties <doc_c_sharp_exports>` of the
  60. type ``Stats`` will only allow instances of this resource type to be assigned, and the inspector
  61. will let you create and load instances of this type easily.
  62. .. image:: img/globalclasses_exportedproperty1.webp
  63. .. image:: img/globalclasses_exportedproperty2.webp
  64. .. warning::
  65. The Godot editor will hide these custom classes with names that begin with the prefix
  66. "Editor" in the "Create New Node" or "Create New Scene" dialog windows. The classes
  67. are available for instantiation at runtime via their class names, but are
  68. automatically hidden by the editor windows along with the built-in editor nodes used
  69. by the Godot editor.