c_sharp_global_classes.rst 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. These classes show up in the *Add Node* and *Create Resource* dialogs,
  6. and :ref:`exported properties <doc_c_sharp_exports>` are restricted to instances of the global class or derived classes.
  7. Global classes are registered with the ``[GlobalClass]`` attribute.
  8. .. code-block:: csharp
  9. using Godot;
  10. [GlobalClass]
  11. public partial class MyNode : Node
  12. {
  13. }
  14. The ``MyNode`` type will be registered as a global class with the same name as the type's name.
  15. .. image:: img/globalclasses_addnode.webp
  16. The ``[Icon]`` attribute also allows to provide the path to an icon so it can
  17. be used as the class' icon in the editor.
  18. .. code-block:: csharp
  19. using Godot;
  20. [GlobalClass, Icon("res://Stats/StatsIcon.svg")]
  21. public partial class Stats : Resource
  22. {
  23. [Export]
  24. public int Strength { get; set; }
  25. [Export]
  26. public int Defense { get; set; }
  27. [Export]
  28. public int Speed { get; set; }
  29. }
  30. .. image:: img/globalclasses_createresource.webp
  31. The ``Stats`` class is a custom resource registered as a global class. :ref:`Exporting properties <doc_c_sharp_exports>` of the
  32. type ``Stats`` will only allow instances of this resource type to be assigned, and the inspector
  33. will let you create and load instances of this type easily.
  34. .. image:: img/globalclasses_exportedproperty1.webp
  35. .. image:: img/globalclasses_exportedproperty2.webp