c_sharp_exports.rst 9.3 KB

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