gdscript_exports.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. .. _doc_gdscript_exports:
  2. GDScript 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`` annotation::
  10. extends Button
  11. @export var number = 5
  12. In that example the value `5` will be saved and visible in the property editor.
  13. An exported variable must be initialized to a constant expression or have a type specifier
  14. in the variable. Some of the export annotations have a specific type and don't need the variable to be typed (see the
  15. *Examples* section below).
  16. One of the fundamental benefits of exporting member variables is to have
  17. them visible and editable in the 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 other languages such as C#.
  23. The syntax varies depending on the language. See :ref:`doc_c_sharp_exports`
  24. for information on C# exports.
  25. Basic use
  26. ---------
  27. If the exported value assigns a constant or constant expression,
  28. the type will be inferred and used in the editor.
  29. ::
  30. @export var number = 5
  31. If there's no default value, you can add a type to the variable.
  32. ::
  33. @export var number: int
  34. Export works with resource types.
  35. ::
  36. @export var character_face: Texture
  37. @export var scene_file: PackedScene
  38. There are many resource types that can be used this way, try e.g.
  39. the following to list them:
  40. ::
  41. @export var resource: Resource
  42. Integers and strings hint enumerated values.
  43. ::
  44. # Editor will enumerate as 0, 1 and 2.
  45. @export_enum("Warrior", "Magician", "Thief") var character_class
  46. If type is String, editor will enumerate with string names.
  47. ::
  48. @export_enum("Rebecca", "Mary", "Leah") var character_name: String
  49. Named enum values
  50. -----------------
  51. Editor will enumerate as THING_1, THING_2, ANOTHER_THING.
  52. ::
  53. enum NamedEnum {THING_1, THING_2, ANOTHER_THING = -1}
  54. @export var x: NamedEnum
  55. Strings as paths
  56. ----------------
  57. String as a path to a file.
  58. ::
  59. @export_file var f
  60. String as a path to a directory.
  61. ::
  62. @export_dir var f
  63. String as a path to a file, custom filter provided as hint.
  64. ::
  65. @export_file("*.txt") var f
  66. Using paths in the global filesystem is also possible,
  67. but only in scripts in tool mode.
  68. String as a path to a PNG file in the global filesystem.
  69. ::
  70. @export_global_file("*.png") var tool_image
  71. String as a path to a directory in the global filesystem.
  72. ::
  73. @export_global_dir var tool_dir
  74. The multiline annotation tells the editor to show a large input
  75. field for editing over multiple lines.
  76. ::
  77. @export_multiline var text
  78. Limiting editor input ranges
  79. ----------------------------
  80. Allow integer values from 0 to 20.
  81. ::
  82. @export_range(0, 20) var i
  83. Allow integer values from -10 to 20.
  84. ::
  85. @export_range(-10, 20) var j
  86. Allow floats from -10 to 20 and snap the value to multiples of 0.2.
  87. ::
  88. @export_range(-10, 20, 0.2) var k: float
  89. The limits can be only for the slider if you add the hints "or_greater" and/or "or_lesser".
  90. ::
  91. @export_range(0, 100, 1, "or_greater", "or_lesser")
  92. .. TODO: Document other hint strings usable with export_range.
  93. Floats with easing hint
  94. -----------------------
  95. Display a visual representation of the 'ease()' function
  96. when editing.
  97. ::
  98. @export_exp_easing var transition_speed
  99. Colors
  100. ------
  101. Regular color given as red-green-blue-alpha value.
  102. ::
  103. @export var col: Color
  104. Color given as red-green-blue value (alpha will always be 1).
  105. ::
  106. @export_color_no_alpha var col: Color
  107. Nodes
  108. -----
  109. Since Godot 4.0, nodes can be directly exported as properties in a script
  110. without having to use NodePaths:
  111. ::
  112. # Allows any node.
  113. @export var node: Node
  114. # Allows any node that inherits from BaseButton.
  115. # Custom classes declared with `class_name` can also be used.
  116. @export var some_button: BaseButton
  117. Exporting NodePaths like in Godot 3.x is still possible, in case you need it:
  118. ::
  119. @export var node_path: NodePath
  120. var node = get_node(node_path)
  121. If you want to limit the types of nodes for NodePaths, you can use the
  122. :ref:`@export_node_path<class_@GDScript_annotation_@export_node_path>`
  123. annotation:
  124. ::
  125. @export_node_path(Button, TouchScreenButton) var some_button
  126. Resources
  127. ---------
  128. ::
  129. @export var resource: Resource
  130. In the Inspector, you can then drag and drop a resource file
  131. from the FileSystem dock into the variable slot.
  132. Opening the inspector dropdown may result in an
  133. extremely long list of possible classes to create, however.
  134. Therefore, if you specify an extension of Resource such as:
  135. ::
  136. @export var resource: AnimationNode
  137. The drop-down menu will be limited to AnimationNode and all
  138. its inherited classes.
  139. It must be noted that even if the script is not being run while in the
  140. editor, the exported properties are still editable. This can be used
  141. in conjunction with a :ref:`script in "tool" mode <doc_gdscript_tool_mode>`.
  142. Exporting bit flags
  143. -------------------
  144. Integers used as bit flags can store multiple ``true``/``false`` (boolean)
  145. values in one property. By using the ``@export_flags`` annotation, they
  146. can be set from the editor::
  147. # Set any of the given flags from the editor.
  148. @export_flags("Fire", "Water", "Earth", "Wind") var spell_elements = 0
  149. You must provide a string description for each flag. In this example, ``Fire``
  150. has value 1, ``Water`` has value 2, ``Earth`` has value 4 and ``Wind``
  151. corresponds to value 8. Usually, constants should be defined accordingly (e.g.
  152. ``const ELEMENT_WIND = 8`` and so on).
  153. Export annotations are also provided for the physics, render, and navigation layers defined in the project settings::
  154. @export_flags_2d_physics var layers_2d_physics
  155. @export_flags_2d_render var layers_2d_render
  156. @export_flags_2d_navigation var layers_2d_navigation
  157. @export_flags_3d_physics var layers_3d_physics
  158. @export_flags_3d_render var layers_3d_render
  159. @export_flags_3d_navigation var layers_3d_navigation
  160. Using bit flags requires some understanding of bitwise operations.
  161. If in doubt, use boolean variables instead.
  162. Exporting arrays
  163. ----------------
  164. Exported arrays can have initializers, but they must be constant expressions.
  165. If the exported array specifies a type which inherits from Resource, the array
  166. values can be set in the inspector by dragging and dropping multiple files
  167. from the FileSystem dock at once.
  168. The default value **must** be a constant expression.
  169. ::
  170. @export var a = [1, 2, 3]
  171. Exported arrays can specify type (using the same hints as before).
  172. ::
  173. @export var ints: Array[int] = [1, 2, 3]
  174. # Nested typed arrays such as `Array[Array[float]]` are not supported yet.
  175. @export var two_dimensional: Array[Array] = [[1.0, 2.0], [3.0, 4.0]]
  176. You can omit the default value, but it would then be ``null`` if not assigned.
  177. ::
  178. @export var b: Array
  179. @export var scenes: Array[PackedScene]
  180. Arrays with specified types which inherit from resource can be set by
  181. drag-and-dropping multiple files from the FileSystem dock.
  182. ::
  183. @export var textures: Array[Texture] = []
  184. @export var scenes: Array[PackedScene] = []
  185. Packed type arrays also work, but only initialized empty:
  186. ::
  187. @export var vector3s = PackedVector3Array()
  188. @export var strings = PackedStringArray()
  189. Setting exported variables from a tool script
  190. ---------------------------------------------
  191. When changing an exported variable's value from a script in
  192. :ref:`doc_gdscript_tool_mode`, the value in the inspector won't be updated
  193. automatically. To update it, call
  194. :ref:`notify_property_list_changed() <class_Object_method_notify_property_list_changed>`
  195. after setting the exported variable's value.
  196. Advanced exports
  197. ----------------
  198. Not every type of export can be provided on the level of the language itself to
  199. avoid unnecessary design complexity. The following describes some more or less
  200. common exporting features which can be implemented with a low-level API.
  201. Before reading further, you should get familiar with the way properties are
  202. handled and how they can be customized with
  203. :ref:`_set() <class_Object_method__get_property_list>`,
  204. :ref:`_get() <class_Object_method__get_property_list>`, and
  205. :ref:`_get_property_list() <class_Object_method__get_property_list>` methods as
  206. described in :ref:`doc_accessing_data_or_logic_from_object`.
  207. .. seealso:: For binding properties using the above methods in C++, see
  208. :ref:`doc_binding_properties_using_set_get_property_list`.
  209. .. warning:: The script must operate in the ``tool`` mode so the above methods
  210. can work from within the editor.