c_sharp_exports.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. .. _doc_c_sharp_exports:
  2. C# exports
  3. ==========
  4. Introduction to exports
  5. -----------------------
  6. In Godot, class members can be exported. This means their value gets saved along
  7. with the resource (such as the :ref:`scene <class_PackedScene>`) they're
  8. attached to. They will also be available for editing in the property editor.
  9. Exporting is done by using the ``[Export]`` attribute.
  10. .. code-block:: csharp
  11. using Godot;
  12. public partial class ExportExample : Node3D
  13. {
  14. [Export]
  15. private int Number = 5;
  16. }
  17. In that example the value ``5`` will be saved, and after building the current project
  18. it will be visible in the property editor. This way, artists and game designers
  19. can modify values that later influence how the program runs. For this, a
  20. special export syntax is provided.
  21. Exporting can only be done with built-in types or objects derived from the :ref:`Resource class <class_Resource>`.
  22. .. note::
  23. Exporting properties can also be done in GDScript, for information on that
  24. see :ref:`doc_gdscript_exports`.
  25. Basic use
  26. ---------
  27. Exporting can work with and without setting a default value. For int and float
  28. ``0`` will then be used as the default.
  29. .. code-block:: csharp
  30. [Export]
  31. private int Number;
  32. Export works with resource types.
  33. .. code-block:: csharp
  34. [Export]
  35. private Texture CharacterFace;
  36. [Export]
  37. private PackedScene SceneFile;
  38. There are many resource types that can be used this way, try e.g.
  39. the following to list them:
  40. .. code-block:: csharp
  41. [Export]
  42. private Resource Resource;
  43. Grouping exports
  44. ----------------
  45. It is possible to group your exported properties inside the Inspector with the ``[ExportGroup]``
  46. attribute. Every exported property after this attribute will be added to the group. Start a new
  47. group or use ``[ExportGroup("")]`` to break out.
  48. .. code-block:: csharp
  49. [ExportGroup("My Properties")]
  50. [Export]
  51. private int Number = 3;
  52. The second argument of the attribute can be used to only group properties with the specified prefix.
  53. Groups cannot be nested, use ``[ExportSubgroup]`` to create subgroups within a group.
  54. .. code-block:: csharp
  55. [ExportSubgroup("Extra Properties")]
  56. [Export]
  57. private string Text = "";
  58. [Export]
  59. private bool Flag = false;
  60. You can also change the name of your main category, or create additional categories in the property
  61. list with the ``[ExportCategory]`` attribute.
  62. .. code-block:: csharp
  63. [ExportCategory("Main Category")]
  64. [Export]
  65. private int Number = 3;
  66. [Export]
  67. private string Text = "";
  68. [ExportCategory("Extra Category")]
  69. [Export]
  70. private bool Flag = false;
  71. .. note::
  72. The list of properties is organized based on the class inheritance, and new categories break
  73. that expectation. Use them carefully, especially when creating projects for public use.
  74. ..
  75. Commenting out enum examples because I have been told they
  76. require extra steps to actually work properly. The examples below
  77. will show up in the inspector but apparently do not function properly
  78. ..
  79. Integers and strings hint enumerated values.
  80. ..
  81. code-block:: csharp
  82. ..
  83. // Editor will enumerate as 0, 1 and 2.
  84. [Export(PropertyHint.Enum, "Warrior,Magician,Thief")]
  85. private int CharacterClass;
  86. ..
  87. If type is String, editor will enumerate with string names.
  88. ..
  89. code-block:: csharp
  90. ..
  91. [Export(PropertyHint.Enum, "Rebecca,Mary,Leah")]
  92. private string CharacterName;
  93. ..
  94. Named enum values
  95. -----------------
  96. ..
  97. Editor will enumerate as THING_1, THING_2, ANOTHER_THING.
  98. ..
  99. code-block:: csharp
  100. ..
  101. private enum NamedEnum
  102. {
  103. Thing1,
  104. Thing2,
  105. AnotherThing = -1
  106. }
  107. [Export(PropertyHint.Enum)]
  108. private NamedEnum X;
  109. Strings as paths
  110. ----------------
  111. Property hints can be used to export strings as paths
  112. String as a path to a file.
  113. .. code-block:: csharp
  114. [Export(PropertyHint.File)]
  115. private string GameFile;
  116. String as a path to a directory.
  117. .. code-block:: csharp
  118. [Export(PropertyHint.Dir)]
  119. private string GameDirectory;
  120. String as a path to a file, custom filter provided as hint.
  121. .. code-block:: csharp
  122. [Export(PropertyHint.File, "*.txt,")]
  123. private string GameFile;
  124. Using paths in the global filesystem is also possible,
  125. but only in scripts in tool mode.
  126. String as a path to a PNG file in the global filesystem.
  127. .. code-block:: csharp
  128. [Export(PropertyHint.GlobalFile, "*.png")]
  129. private string ToolImage;
  130. String as a path to a directory in the global filesystem.
  131. .. code-block:: csharp
  132. [Export(PropertyHint.GlobalDir)]
  133. private string ToolDir;
  134. The multiline annotation tells the editor to show a large input
  135. field for editing over multiple lines.
  136. .. code-block:: csharp
  137. [Export(PropertyHint.MultilineText)]
  138. private string Text;
  139. Limiting editor input ranges
  140. ----------------------------
  141. Using the range property hint allows you to limit what can be
  142. input as a value using the editor.
  143. Allow integer values from 0 to 20.
  144. .. code-block:: csharp
  145. [Export(PropertyHint.Range, "0,20,")]
  146. private int Number;
  147. Allow integer values from -10 to 20.
  148. .. code-block:: csharp
  149. [Export(PropertyHint.Range, "-10,20,")]
  150. private int Number;
  151. Allow floats from -10 to 20 and snap the value to multiples of 0.2.
  152. .. code-block:: csharp
  153. [Export(PropertyHint.Range, "-10,20,0.2")]
  154. private float Number;
  155. If you add the hints "or_greater" and/or "or_lesser" you can go above
  156. or below the limits when adjusting the value by typing it instead of using
  157. the slider.
  158. .. code-block:: csharp
  159. [Export(PropertyHint.Range, "0,100,1,or_greater,or_lesser")]
  160. private int Number;
  161. Allow values 'y = exp(x)' where 'y' varies between 100 and 1000
  162. while snapping to steps of 20. The editor will present a
  163. slider for easily editing the value. This only works with floats.
  164. .. code-block:: csharp
  165. [Export(PropertyHint.ExpRange, "100,1000,20")]
  166. private float Number;
  167. Floats with easing hint
  168. -----------------------
  169. Display a visual representation of the 'ease()' function
  170. when editing.
  171. .. code-block:: csharp
  172. [Export(PropertyHint.ExpEasing)]
  173. private float TransitionSpeed;
  174. Colors
  175. ------
  176. Regular color given as red-green-blue-alpha value.
  177. .. code-block:: csharp
  178. [Export]
  179. private Color Col;
  180. Color given as red-green-blue value (alpha will always be 1).
  181. .. code-block:: csharp
  182. [Export(PropertyHint.ColorNoAlpha)]
  183. private Color Col;
  184. Nodes
  185. -----
  186. Nodes can't be directly exported. Instead you need to export
  187. a node path, then use that node path with ``GetNode()``.
  188. .. code-block:: csharp
  189. [Export]
  190. private NodePath MyNodePath;
  191. private Label MyNode;
  192. public override void _Ready()
  193. {
  194. MyNode = GetNode<Label>(MyNodePath);
  195. }
  196. Resources
  197. ---------
  198. .. code-block:: csharp
  199. [Export]
  200. private Resource Resource;
  201. In the Inspector, you can then drag and drop a resource file
  202. from the FileSystem dock into the variable slot.
  203. Opening the inspector dropdown may result in an
  204. extremely long list of possible classes to create, however.
  205. Therefore, if you specify an extension of Resource such as:
  206. .. code-block:: csharp
  207. [Export]
  208. private AnimationNode Resource;
  209. The drop-down menu will be limited to AnimationNode and all
  210. its inherited classes.
  211. It must be noted that even if the script is not being run while in the
  212. editor, the exported properties are still editable. This can be used
  213. in conjunction with a :ref:`script in "tool" mode <doc_gdscript_tool_mode>`.
  214. Exporting bit flags
  215. -------------------
  216. Integers used as bit flags can store multiple ``true``/``false`` (boolean)
  217. values in one property. By using the ``Flags`` property hint, they
  218. can be set from the editor.
  219. .. code-block:: csharp
  220. // Set any of the given flags from the editor.
  221. [Export(PropertyHint.Flags, "Fire,Water,Earth,Wind")]
  222. private int SpellElements = 0;
  223. You must provide a string description for each flag. In this example, ``Fire``
  224. has value 1, ``Water`` has value 2, ``Earth`` has value 4 and ``Wind``
  225. corresponds to value 8. Usually, constants should be defined accordingly (e.g.
  226. ``private const int ElementWind = 8`` and so on).
  227. Export annotations are also provided for the physics and render layers defined in the project settings.
  228. .. code-block:: csharp
  229. [Export(PropertyHint.Layers2DPhysics)]
  230. private int Layers2DPhysics;
  231. [Export(PropertyHint.Layers2DRender)]
  232. private int Layers2DRender;
  233. [Export(PropertyHint.Layers3DPhysics)]
  234. private int layers3DPhysics;
  235. [Export(PropertyHint.Layers3DRender)]
  236. private int layers3DRender;
  237. Using bit flags requires some understanding of bitwise operations.
  238. If in doubt, use boolean variables instead.
  239. Exporting arrays
  240. ----------------
  241. Exported arrays should be initialized empty.
  242. .. code-block:: csharp
  243. [Export]
  244. private Vector3[] Vector3s = System.Array.Empty<Vector3>();
  245. [Export]
  246. private string[] Strings = System.Array.Empty<string>();
  247. You can omit the default value, but then it would be null if not assigned.
  248. .. code-block:: csharp
  249. [Export]
  250. private int[] Numbers;
  251. Arrays with specified types which inherit from resource can be set by
  252. drag-and-dropping multiple files from the FileSystem dock.
  253. .. code-block:: csharp
  254. [Export]
  255. private Texture[] Textures;
  256. [Export]
  257. private PackedScene[] Scenes;
  258. Arrays where the default value includes run-time values can't
  259. be exported.
  260. .. code-block:: csharp
  261. private int Number = 1;
  262. private int[] SeveralNumbers = {Number,2,3};
  263. Setting exported variables from a tool script
  264. ---------------------------------------------
  265. When changing an exported variable's value from a script in
  266. :ref:`doc_gdscript_tool_mode`, the value in the inspector won't be updated
  267. automatically. To update it, call
  268. :ref:`notify_property_list_changed() <class_Object_method_notify_property_list_changed>`
  269. after setting the exported variable's value.
  270. Advanced exports
  271. ----------------
  272. Not every type of export can be provided on the level of the language itself to
  273. avoid unnecessary design complexity. The following describes some more or less
  274. common exporting features which can be implemented with a low-level API.
  275. Before reading further, you should get familiar with the way properties are
  276. handled and how they can be customized with
  277. :ref:`_set() <class_Object_method__get_property_list>`,
  278. :ref:`_get() <class_Object_method__get_property_list>`, and
  279. :ref:`_get_property_list() <class_Object_method__get_property_list>` methods as
  280. described in :ref:`doc_accessing_data_or_logic_from_object`.
  281. .. seealso:: For binding properties using the above methods in C++, see
  282. :ref:`doc_binding_properties_using_set_get_property_list`.
  283. .. warning:: The script must operate in the ``tool`` mode so the above methods
  284. can work from within the editor.