c_sharp_exports.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. ..
  44. Commenting out enum examples because I have been told they
  45. require extra steps to actually work properly. The examples below
  46. will show up in the inspector but apparently do not function properly
  47. ..
  48. Integers and strings hint enumerated values.
  49. ..
  50. code-block:: csharp
  51. ..
  52. // Editor will enumerate as 0, 1 and 2.
  53. [Export(PropertyHint.Enum, "Warrior,Magician,Thief")]
  54. private int CharacterClass;
  55. ..
  56. If type is String, editor will enumerate with string names.
  57. ..
  58. code-block:: csharp
  59. ..
  60. [Export(PropertyHint.Enum, "Rebecca,Mary,Leah")]
  61. private string CharacterName;
  62. ..
  63. Named enum values
  64. -----------------
  65. ..
  66. Editor will enumerate as THING_1, THING_2, ANOTHER_THING.
  67. ..
  68. code-block:: csharp
  69. ..
  70. private enum NamedEnum
  71. {
  72. Thing1,
  73. Thing2,
  74. AnotherThing = -1
  75. }
  76. [Export(PropertyHint.Enum)]
  77. private NamedEnum X;
  78. Strings as paths
  79. ----------------
  80. Property hints can be used to export strings as paths
  81. String as a path to a file.
  82. .. code-block:: csharp
  83. [Export(PropertyHint.File)]
  84. private string GameFile;
  85. String as a path to a directory.
  86. .. code-block:: csharp
  87. [Export(PropertyHint.Dir)]
  88. private string GameDirectory;
  89. String as a path to a file, custom filter provided as hint.
  90. .. code-block:: csharp
  91. [Export(PropertyHint.File, "*.txt,")]
  92. private string GameFile;
  93. Using paths in the global filesystem is also possible,
  94. but only in scripts in tool mode.
  95. String as a path to a PNG file in the global filesystem.
  96. .. code-block:: csharp
  97. [Export(PropertyHint.GlobalFile, "*.png")]
  98. private string ToolImage;
  99. String as a path to a directory in the global filesystem.
  100. .. code-block:: csharp
  101. [Export(PropertyHint.GlobalDir)]
  102. private string ToolDir;
  103. The multiline annotation tells the editor to show a large input
  104. field for editing over multiple lines.
  105. .. code-block:: csharp
  106. [Export(PropertyHint.MultilineText)]
  107. private string Text;
  108. Limiting editor input ranges
  109. ----------------------------
  110. Using the range property hint allows you to limit what can be
  111. input as a value using the editor.
  112. Allow integer values from 0 to 20.
  113. .. code-block:: csharp
  114. [Export(PropertyHint.Range, "0,20,")]
  115. private int Number;
  116. Allow integer values from -10 to 20.
  117. .. code-block:: csharp
  118. [Export(PropertyHint.Range, "-10,20,")]
  119. private int Number;
  120. Allow floats from -10 to 20 and snap the value to multiples of 0.2.
  121. .. code-block:: csharp
  122. [Export(PropertyHint.Range, "-10,20,0.2")]
  123. private float Number;
  124. If you add the hints "or_greater" and/or "or_lesser" you can go above
  125. or below the limits when adjusting the value by typing it instead of using
  126. the slider.
  127. .. code-block:: csharp
  128. [Export(PropertyHint.Range, "0,100,1,or_greater,or_lesser")]
  129. private int Number;
  130. Allow values 'y = exp(x)' where 'y' varies between 100 and 1000
  131. while snapping to steps of 20. The editor will present a
  132. slider for easily editing the value. This only works with floats.
  133. .. code-block:: csharp
  134. [Export(PropertyHint.ExpRange, "100,1000,20")]
  135. private float Number;
  136. Floats with easing hint
  137. -----------------------
  138. Display a visual representation of the 'ease()' function
  139. when editing.
  140. .. code-block:: csharp
  141. [Export(PropertyHint.ExpEasing)]
  142. private float TransitionSpeed;
  143. Colors
  144. ------
  145. Regular color given as red-green-blue-alpha value.
  146. .. code-block:: csharp
  147. [Export]
  148. private Color Col;
  149. Color given as red-green-blue value (alpha will always be 1).
  150. .. code-block:: csharp
  151. [Export(PropertyHint.ColorNoAlpha)]
  152. private Color Col;
  153. Nodes
  154. -----
  155. Nodes can't be directly exported. Instead you need to export
  156. a node path, then use that node path with ``GetNode()``.
  157. .. code-block:: csharp
  158. [Export]
  159. private NodePath MyNodePath;
  160. private Label MyNode;
  161. public override void _Ready()
  162. {
  163. MyNode = GetNode<Label>(MyNodePath);
  164. }
  165. Resources
  166. ---------
  167. .. code-block:: csharp
  168. [Export]
  169. private Resource Resource;
  170. In the Inspector, you can then drag and drop a resource file
  171. from the FileSystem dock into the variable slot.
  172. Opening the inspector dropdown may result in an
  173. extremely long list of possible classes to create, however.
  174. Therefore, if you specify an extension of Resource such as:
  175. .. code-block:: csharp
  176. [Export]
  177. private AnimationNode Resource;
  178. The drop-down menu will be limited to AnimationNode and all
  179. its inherited classes.
  180. It must be noted that even if the script is not being run while in the
  181. editor, the exported properties are still editable. This can be used
  182. in conjunction with a :ref:`script in "tool" mode <doc_gdscript_tool_mode>`.
  183. Exporting bit flags
  184. -------------------
  185. Integers used as bit flags can store multiple ``true``/``false`` (boolean)
  186. values in one property. By using the ``Flags`` property hint, they
  187. can be set from the editor.
  188. .. code-block:: csharp
  189. // Set any of the given flags from the editor.
  190. [Export(PropertyHint.Flags, "Fire,Water,Earth,Wind")]
  191. private int SpellElements = 0;
  192. You must provide a string description for each flag. In this example, ``Fire``
  193. has value 1, ``Water`` has value 2, ``Earth`` has value 4 and ``Wind``
  194. corresponds to value 8. Usually, constants should be defined accordingly (e.g.
  195. ``private const int ElementWind = 8`` and so on).
  196. Export annotations are also provided for the physics and render layers defined in the project settings.
  197. .. code-block:: csharp
  198. [Export(PropertyHint.Layers2dPhysics)]
  199. private int Layers2dPhysics;
  200. [Export(PropertyHint.Layers2dRender)]
  201. private int Layers2dRender;
  202. [Export(PropertyHint.Layers3dPhysics)]
  203. private int layers3dPhysics;
  204. [Export(PropertyHint.Layers3dRender)]
  205. private int layers3dRender;
  206. Using bit flags requires some understanding of bitwise operations.
  207. If in doubt, use boolean variables instead.
  208. Exporting arrays
  209. ----------------
  210. Exported arrays should be initialized empty.
  211. .. code-block:: csharp
  212. [Export]
  213. private Vector3[] Vector3s = System.Array.Empty<Vector3>();
  214. [Export]
  215. private string[] Strings = System.Array.Empty<string>();
  216. You can omit the default value, but then it would be null if not assigned.
  217. .. code-block:: csharp
  218. [Export]
  219. private int[] Numbers;
  220. Arrays with specified types which inherit from resource can be set by
  221. drag-and-dropping multiple files from the FileSystem dock.
  222. .. code-block:: csharp
  223. [Export]
  224. private Texture[] Textures;
  225. [Export]
  226. private PackedScene[] Scenes;
  227. Arrays where the default value includes run-time values can't
  228. be exported.
  229. .. code-block:: csharp
  230. private int Number = 1;
  231. private int[] SeveralNumbers = {Number,2,3};
  232. Setting exported variables from a tool script
  233. ---------------------------------------------
  234. When changing an exported variable's value from a script in
  235. :ref:`doc_gdscript_tool_mode`, the value in the inspector won't be updated
  236. automatically. To update it, call
  237. :ref:`notify_property_list_changed() <class_Object_method_notify_property_list_changed>`
  238. after setting the exported variable's value.
  239. Advanced exports
  240. ----------------
  241. Not every type of export can be provided on the level of the language itself to
  242. avoid unnecessary design complexity. The following describes some more or less
  243. common exporting features which can be implemented with a low-level API.
  244. Before reading further, you should get familiar with the way properties are
  245. handled and how they can be customized with
  246. :ref:`_set() <class_Object_method__get_property_list>`,
  247. :ref:`_get() <class_Object_method__get_property_list>`, and
  248. :ref:`_get_property_list() <class_Object_method__get_property_list>` methods as
  249. described in :ref:`doc_accessing_data_or_logic_from_object`.
  250. .. seealso:: For binding properties using the above methods in C++, see
  251. :ref:`doc_binding_properties_using_set_get_property_list`.
  252. .. warning:: The script must operate in the ``tool`` mode so the above methods
  253. can work from within the editor.