c_sharp_global_classes.rst 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 editor so they can be used
  5. more conveniently.
  6. - Global classes show up in the *Add Node* and *Create Resource* dialogs.
  7. - If an :ref:`exported property <doc_c_sharp_exports>` is a global class, the
  8. inspector restricts assignment, allowing only instances of that global class
  9. or any derived classes.
  10. Global classes are registered with the ``[GlobalClass]`` attribute.
  11. .. code-block:: csharp
  12. using Godot;
  13. [GlobalClass]
  14. public partial class MyNode : Node
  15. {
  16. }
  17. The ``MyNode`` type will be registered as a global class with the same name as the type's name.
  18. .. image:: img/globalclasses_addnode.webp
  19. The *Select a Node* window for the ``MyNode`` exported property filters the list
  20. of nodes in the scene to match the assignment restriction.
  21. .. code-block:: csharp
  22. public partial class Main : Node
  23. {
  24. [Export]
  25. public MyNode MyNode { get; set; }
  26. }
  27. .. image:: img/globalclasses_exportednode.webp
  28. If a custom type isn't registered as a global class, the assignment is
  29. restricted to the Godot type the custom type is based on. For example, inspector
  30. assignments to an export of the type ``MySimpleSprite2D`` are restricted to
  31. ``Sprite2D`` and derived types.
  32. .. code-block:: csharp
  33. public partial class MySimpleSprite2D : Sprite2D
  34. {
  35. }
  36. When combined with the ``[GlobalClass]`` attribute, the ``[Icon]`` attribute
  37. allows providing a path to an icon to show when the class is displayed in the
  38. editor.
  39. .. code-block:: csharp
  40. using Godot;
  41. [GlobalClass, Icon("res://Stats/StatsIcon.svg")]
  42. public partial class Stats : Resource
  43. {
  44. [Export]
  45. public int Strength { get; set; }
  46. [Export]
  47. public int Defense { get; set; }
  48. [Export]
  49. public int Speed { get; set; }
  50. }
  51. .. image:: img/globalclasses_createresource.webp
  52. The ``Stats`` class is a custom resource registered as a global class. :ref:`Exporting properties <doc_c_sharp_exports>` of the
  53. type ``Stats`` will only allow instances of this resource type to be assigned, and the inspector
  54. will let you create and load instances of this type easily.
  55. .. image:: img/globalclasses_exportedproperty1.webp
  56. .. image:: img/globalclasses_exportedproperty2.webp
  57. .. warning::
  58. The Godot editor will hide these custom classes with names that beging with the prefix
  59. "Editor" in the 'Create New Node' or 'Create New Scene' dialog windows. The classes
  60. are available for instantiation at runtime via their class names, but are
  61. automatically hidden by the editor windows along with the built-in editor nodes used
  62. by the Godot editor.