gdscript_exports.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. .. _doc_gdscript_exports:
  2. GDScript exported properties
  3. ============================
  4. In Godot, class members can be exported. This means their value gets saved along
  5. with the resource (such as the :ref:`scene <class_PackedScene>`) they're
  6. attached to. They will also be available for editing in the property editor.
  7. Exporting is done by using the ``@export`` annotation.
  8. ::
  9. @export var number: int = 5
  10. In that example the value ``5`` will be saved and visible in the property editor.
  11. An exported variable must be initialized to a constant expression or have a type specifier
  12. in the variable. Some of the export annotations have a specific type and don't need the variable to be typed (see the
  13. *Examples* section below).
  14. One of the fundamental benefits of exporting member variables is to have
  15. them visible and editable in the editor. This way, artists and game designers
  16. can modify values that later influence how the program runs. For this, a
  17. special export syntax is provided.
  18. .. note::
  19. Exporting properties can also be done in other languages such as C#.
  20. The syntax varies depending on the language. See :ref:`doc_c_sharp_exports`
  21. for information on C# exports.
  22. Basic use
  23. ---------
  24. If the exported value assigns a constant or constant expression,
  25. the type will be inferred and used in the editor.
  26. ::
  27. @export var number = 5
  28. If there's no default value, you can add a type to the variable.
  29. ::
  30. @export var number: int
  31. Resources and nodes can be exported.
  32. ::
  33. @export var resource: Resource
  34. @export var node: Node
  35. Grouping Exports
  36. ----------------
  37. It is possible to group your exported properties inside the Inspector
  38. with the :ref:`@export_group <class_@GDScript_annotation_@export_group>`
  39. annotation. Every exported property after this annotation will be added to
  40. the group. Start a new group or use ``@export_group("")`` to break out.
  41. ::
  42. @export_group("My Properties")
  43. @export var number = 3
  44. The second argument of the annotation can be used to only group properties
  45. with the specified prefix.
  46. Groups cannot be nested, use :ref:`@export_subgroup <class_@GDScript_annotation_@export_subgroup>`
  47. to create subgroups within a group.
  48. ::
  49. @export_subgroup("Extra Properties")
  50. @export var string = ""
  51. @export var flag = false
  52. You can also change the name of your main category, or create additional
  53. categories in the property list with the :ref:`@export_category <class_@GDScript_annotation_@export_category>`
  54. annotation.
  55. ::
  56. @export_category("Main Category")
  57. @export var number = 3
  58. @export var string = ""
  59. @export_category("Extra Category")
  60. @export var flag = false
  61. .. note::
  62. The list of properties is organized based on the class inheritance and
  63. new categories break that expectation. Use them carefully, especially
  64. when creating projects for public use.
  65. Strings as paths
  66. ----------------
  67. String as a path to a file.
  68. ::
  69. @export_file var f
  70. String as a path to a directory.
  71. ::
  72. @export_dir var f
  73. String as a path to a file, custom filter provided as hint.
  74. ::
  75. @export_file("*.txt") var f
  76. Using paths in the global filesystem is also possible,
  77. but only in scripts in tool mode.
  78. String as a path to a PNG file in the global filesystem.
  79. ::
  80. @export_global_file("*.png") var tool_image
  81. String as a path to a directory in the global filesystem.
  82. ::
  83. @export_global_dir var tool_dir
  84. The multiline annotation tells the editor to show a large input
  85. field for editing over multiple lines.
  86. ::
  87. @export_multiline var text
  88. Limiting editor input ranges
  89. ----------------------------
  90. Allow integer values from 0 to 20.
  91. ::
  92. @export_range(0, 20) var i
  93. Allow integer values from -10 to 20.
  94. ::
  95. @export_range(-10, 20) var j
  96. Allow floats from -10 to 20 and snap the value to multiples of 0.2.
  97. ::
  98. @export_range(-10, 20, 0.2) var k: float
  99. The limits can be only for the slider if you add the hints "or_greater" and/or "or_less".
  100. ::
  101. @export_range(0, 100, 1, "or_greater", "or_less")
  102. .. TODO: Document other hint strings usable with export_range.
  103. Floats with easing hint
  104. -----------------------
  105. Display a visual representation of the 'ease()' function
  106. when editing.
  107. ::
  108. @export_exp_easing var transition_speed
  109. Colors
  110. ------
  111. Regular color given as red-green-blue-alpha value.
  112. ::
  113. @export var col: Color
  114. Color given as red-green-blue value (alpha will always be 1).
  115. ::
  116. @export_color_no_alpha var col: Color
  117. Nodes
  118. -----
  119. Since Godot 4.0, nodes can be directly exported as properties in a script
  120. without having to use NodePaths:
  121. ::
  122. # Allows any node.
  123. @export var node: Node
  124. # Allows any node that inherits from BaseButton.
  125. # Custom classes declared with `class_name` can also be used.
  126. @export var some_button: BaseButton
  127. Exporting NodePaths like in Godot 3.x is still possible, in case you need it:
  128. ::
  129. @export var node_path: NodePath
  130. var node = get_node(node_path)
  131. If you want to limit the types of nodes for NodePaths, you can use the
  132. :ref:`@export_node_path<class_@GDScript_annotation_@export_node_path>`
  133. annotation:
  134. ::
  135. @export_node_path("Button", "TouchScreenButton") var some_button
  136. Resources
  137. ---------
  138. ::
  139. @export var resource: Resource
  140. In the Inspector, you can then drag and drop a resource file
  141. from the FileSystem dock into the variable slot.
  142. Opening the inspector dropdown may result in an
  143. extremely long list of possible classes to create, however.
  144. Therefore, if you specify an extension of Resource such as:
  145. ::
  146. @export var resource: AnimationNode
  147. The drop-down menu will be limited to AnimationNode and all
  148. its inherited classes.
  149. It must be noted that even if the script is not being run while in the
  150. editor, the exported properties are still editable. This can be used
  151. in conjunction with a :ref:`script in "tool" mode <doc_gdscript_tool_mode>`.
  152. Exporting bit flags
  153. -------------------
  154. Integers used as bit flags can store multiple ``true``/``false`` (boolean)
  155. values in one property. By using the ``@export_flags`` annotation, they
  156. can be set from the editor::
  157. # Set any of the given flags from the editor.
  158. @export_flags("Fire", "Water", "Earth", "Wind") var spell_elements = 0
  159. You must provide a string description for each flag. In this example, ``Fire``
  160. has value 1, ``Water`` has value 2, ``Earth`` has value 4 and ``Wind``
  161. corresponds to value 8. Usually, constants should be defined accordingly (e.g.
  162. ``const ELEMENT_WIND = 8`` and so on).
  163. You can add explicit values using a colon::
  164. @export_flags("Self:4", "Allies:8", "Foes:16") var spell_targets = 0
  165. Only power of 2 values are valid as bit flags options. The lowest allowed value
  166. is 1, as 0 means that nothing is selected. You can also add options that are a
  167. combination of other flags::
  168. @export_flags("Self:4", "Allies:8", "Self and Allies:12", "Foes:16")
  169. var spell_targets = 0
  170. Export annotations are also provided for the physics, render, and navigation layers defined in the project settings::
  171. @export_flags_2d_physics var layers_2d_physics
  172. @export_flags_2d_render var layers_2d_render
  173. @export_flags_2d_navigation var layers_2d_navigation
  174. @export_flags_3d_physics var layers_3d_physics
  175. @export_flags_3d_render var layers_3d_render
  176. @export_flags_3d_navigation var layers_3d_navigation
  177. Using bit flags requires some understanding of bitwise operations.
  178. If in doubt, use boolean variables instead.
  179. Exporting enums
  180. ---------------
  181. Properties can be exported with a type hint referencing an enum to limit their values
  182. to the values of the enumeration. The editor will create a widget in the Inspector, enumerating
  183. the following as "Thing 1", "Thing 2", "Another Thing". The value will be stored as an integer.
  184. ::
  185. enum NamedEnum {THING_1, THING_2, ANOTHER_THING = -1}
  186. @export var x: NamedEnum
  187. Integer and string properties can also be limited to a specific list of values using
  188. the :ref:`@export_enum <class_@GDScript_annotation_@export_enum>` annotation.
  189. The editor will create a widget in the Inspector, enumerating the following as Warrior,
  190. Magician, Thief. The value will be stored as an integer, corresponding to the index
  191. of the selected option (i.e. ``0``, ``1``, or ``2``).
  192. ::
  193. @export_enum("Warrior", "Magician", "Thief") var character_class: int
  194. You can add explicit values using a colon::
  195. @export_enum("Slow:30", "Average:60", "Very Fast:200") var character_speed: int
  196. If the type is String, the value will be stored as a string.
  197. ::
  198. @export_enum("Rebecca", "Mary", "Leah") var character_name: String
  199. If you want to set an initial value, you must specify it explicitly::
  200. @export_enum("Rebecca", "Mary", "Leah") var character_name: String = "Rebecca"
  201. Exporting arrays
  202. ----------------
  203. Exported arrays can have initializers, but they must be constant expressions.
  204. If the exported array specifies a type which inherits from Resource, the array
  205. values can be set in the inspector by dragging and dropping multiple files
  206. from the FileSystem dock at once.
  207. The default value **must** be a constant expression.
  208. ::
  209. @export var a = [1, 2, 3]
  210. Exported arrays can specify type (using the same hints as before).
  211. ::
  212. @export var ints: Array[int] = [1, 2, 3]
  213. # Nested typed arrays such as `Array[Array[float]]` are not supported yet.
  214. @export var two_dimensional: Array[Array] = [[1.0, 2.0], [3.0, 4.0]]
  215. You can omit the default value, but it would then be ``null`` if not assigned.
  216. ::
  217. @export var b: Array
  218. @export var scenes: Array[PackedScene]
  219. Arrays with specified types which inherit from resource can be set by
  220. drag-and-dropping multiple files from the FileSystem dock.
  221. ::
  222. @export var textures: Array[Texture] = []
  223. @export var scenes: Array[PackedScene] = []
  224. Packed type arrays also work, but only initialized empty:
  225. ::
  226. @export var vector3s = PackedVector3Array()
  227. @export var strings = PackedStringArray()
  228. Setting exported variables from a tool script
  229. ---------------------------------------------
  230. When changing an exported variable's value from a script in
  231. :ref:`doc_gdscript_tool_mode`, the value in the inspector won't be updated
  232. automatically. To update it, call
  233. :ref:`notify_property_list_changed() <class_Object_method_notify_property_list_changed>`
  234. after setting the exported variable's value.
  235. Advanced exports
  236. ----------------
  237. Not every type of export can be provided on the level of the language itself to
  238. avoid unnecessary design complexity. The following describes some more or less
  239. common exporting features which can be implemented with a low-level API.
  240. Before reading further, you should get familiar with the way properties are
  241. handled and how they can be customized with
  242. :ref:`_set() <class_Object_private_method__set>`,
  243. :ref:`_get() <class_Object_private_method__get>`, and
  244. :ref:`_get_property_list() <class_Object_private_method__get_property_list>` methods as
  245. described in :ref:`doc_accessing_data_or_logic_from_object`.
  246. .. seealso:: For binding properties using the above methods in C++, see
  247. :ref:`doc_binding_properties_using_set_get_property_list`.
  248. .. warning:: The script must operate in the ``tool`` mode so the above methods
  249. can work from within the editor.