gdscript_exports.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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, and get transferred over when using :ref:`RPCs <doc_high_level_multiplayer_rpcs>`.
  7. They will also be available for editing in the property editor. Exporting is done by using
  8. the ``@export`` annotation.
  9. ::
  10. @export var number: int = 5
  11. In that example the value ``5`` will be saved and visible in the property editor.
  12. An exported variable must be initialized to a constant expression or have a type specifier
  13. in the variable. Some of the export annotations have a specific type and don't need the variable to be typed (see the
  14. *Examples* section below).
  15. One of the fundamental benefits of exporting member variables is to have
  16. them visible and editable in the editor. This way, artists and game designers
  17. can modify values that later influence how the program runs. For this, a
  18. special export syntax is provided.
  19. .. note::
  20. Exporting properties can also be done in other languages such as C#.
  21. The syntax varies depending on the language. See :ref:`doc_c_sharp_exports`
  22. for information on C# exports.
  23. Basic use
  24. ---------
  25. If the exported value assigns a constant or constant expression,
  26. the type will be inferred and used in the editor.
  27. ::
  28. @export var number = 5
  29. If there's no default value, you can add a type to the variable.
  30. ::
  31. @export var number: int
  32. Resources and nodes can be exported.
  33. ::
  34. @export var resource: Resource
  35. @export var node: Node
  36. Grouping exports
  37. ----------------
  38. It is possible to group your exported properties inside the Inspector
  39. with the :ref:`@export_group <class_@GDScript_annotation_@export_group>`
  40. annotation. Every exported property after this annotation will be added to
  41. the group. Start a new group or use ``@export_group("")`` to break out.
  42. ::
  43. @export_group("My Properties")
  44. @export var number = 3
  45. The second argument of the annotation can be used to only group properties
  46. with the specified prefix.
  47. Groups cannot be nested, use :ref:`@export_subgroup <class_@GDScript_annotation_@export_subgroup>`
  48. to create subgroups within a group.
  49. ::
  50. @export_subgroup("Extra Properties")
  51. @export var string = ""
  52. @export var flag = false
  53. You can also change the name of your main category, or create additional
  54. categories in the property list with the :ref:`@export_category <class_@GDScript_annotation_@export_category>`
  55. annotation.
  56. ::
  57. @export_category("Main Category")
  58. @export var number = 3
  59. @export var string = ""
  60. @export_category("Extra Category")
  61. @export var flag = false
  62. .. note::
  63. The list of properties is organized based on the class inheritance and
  64. new categories break that expectation. Use them carefully, especially
  65. when creating projects for public use.
  66. Strings as paths
  67. ----------------
  68. String as a path to a file. See :ref:`@export_file <class_@GDScript_annotation_@export_file>`.
  69. ::
  70. @export_file var f
  71. String as a path to a directory. See :ref:`@export_dir <class_@GDScript_annotation_@export_dir>`.
  72. ::
  73. @export_dir var f
  74. String as a path to a file, custom filter provided as hint. See again :ref:`@export_file <class_@GDScript_annotation_@export_file>`.
  75. ::
  76. @export_file("*.txt") var f
  77. Using paths in the global filesystem is also possible,
  78. but only in scripts in tool mode.
  79. String as a path to a PNG file in the global filesystem. See :ref:`@export_global_file <class_@GDScript_annotation_@export_global_file>`.
  80. ::
  81. @export_global_file("*.png") var tool_image
  82. String as a path to a directory in the global filesystem. See :ref:`@export_global_dir <class_@GDScript_annotation_@export_global_dir>`.
  83. ::
  84. @export_global_dir var tool_dir
  85. The multiline annotation tells the editor to show a large input
  86. field for editing over multiple lines. See :ref:`@export_multiline <class_@GDScript_annotation_@export_multiline>`.
  87. ::
  88. @export_multiline var text
  89. Limiting editor input ranges
  90. ----------------------------
  91. See :ref:`@export_range <class_@GDScript_annotation_@export_range>` for all of the following.
  92. Allow integer values from 0 to 20.
  93. ::
  94. @export_range(0, 20) var i
  95. Allow integer values from -10 to 20.
  96. ::
  97. @export_range(-10, 20) var j
  98. Allow floats from -10 to 20 and snap the value to multiples of 0.2.
  99. ::
  100. @export_range(-10, 20, 0.2) var k: float
  101. The limits can be made to affect only the slider if you add the hints ``"or_less"``
  102. and/or ``"or_greater"``. If either these hints are used, it will be possible for
  103. the user to enter any value or drag the value with the mouse when not using
  104. the slider, even if outside the specified range.
  105. ::
  106. @export_range(0, 100, 1, "or_less", "or_greater") var l: int
  107. The ``"exp"`` hint can be used to make a value have an exponential slider
  108. instead of a linear slider. This means that when dragging the slider towards
  109. the right, changes will become progressively faster when dragging the mouse.
  110. This is useful to make editing values that can be either very small or very large
  111. easier, at the cost of being less intuitive.
  112. ::
  113. @export_range(0, 100000, 0.01, "exp") var exponential: float
  114. For values that are meant to represent an easing factor, use
  115. :ref:`doc_gdscript_exports_floats_with_easing_hint` instead.
  116. The ``"hide_slider"`` hint can be used to hide the horizontal bar that
  117. appears below ``float`` properties, or the up/down arrows that appear besides
  118. ``int`` properties:
  119. ::
  120. @export_range(0, 1000, 0.01, "hide_slider") var no_slider: float
  121. Adding suffixes and handling degrees/radians
  122. --------------------------------------------
  123. A suffix can also be defined to make the value more self-explanatory in the
  124. inspector. For example, to define a value that is meant to be configured as
  125. "meters" (``m``) by the user:
  126. ::
  127. @export_range(0, 100, 1, "suffix:m") var m: int
  128. For angles that are stored in radians but displayed as degrees to the user, use
  129. the `"radians_as_degrees"` hint:
  130. ::
  131. @export_range(0, 360, 0.1, "radians_as_degrees") var angle: float
  132. This performs automatic conversion when the value is displayed or modified in
  133. the inspector and also displays a degree (``°``) suffix. This approach is used
  134. by Godot's own `rotation` properties throughout the editor.
  135. If the angle is stored in degrees instead, use the `"degrees"` hint to display
  136. the degree symbol while disabling the automatic degrees-to-radians conversion
  137. when the value is modified from the inspector.
  138. .. _doc_gdscript_exports_floats_with_easing_hint:
  139. Floats with easing hint
  140. -----------------------
  141. Display a visual representation of the ``ease()`` function
  142. when editing. See :ref:`@export_exp_easing <class_@GDScript_annotation_@export_exp_easing>`.
  143. ::
  144. @export_exp_easing var transition_speed
  145. Colors
  146. ------
  147. Regular color given as red-green-blue-alpha value.
  148. ::
  149. @export var col: Color
  150. Color given as red-green-blue value (alpha will always be 1). See :ref:`@export_color_no_alpha <class_@GDScript_annotation_@export_color_no_alpha>`.
  151. ::
  152. @export_color_no_alpha var col: Color
  153. Nodes
  154. -----
  155. Since Godot 4.0, nodes can be directly exported as properties in a script
  156. without having to use NodePaths:
  157. ::
  158. # Allows any node.
  159. @export var node: Node
  160. # Allows any node that inherits from BaseButton.
  161. # Custom classes declared with `class_name` can also be used.
  162. @export var some_button: BaseButton
  163. Exporting NodePaths like in Godot 3.x is still possible, in case you need it:
  164. ::
  165. @export var node_path: NodePath
  166. var node = get_node(node_path)
  167. If you want to limit the types of nodes for NodePaths, you can use the
  168. :ref:`@export_node_path<class_@GDScript_annotation_@export_node_path>`
  169. annotation:
  170. ::
  171. @export_node_path("Button", "TouchScreenButton") var some_button
  172. Resources
  173. ---------
  174. ::
  175. @export var resource: Resource
  176. In the Inspector, you can then drag and drop a resource file
  177. from the FileSystem dock into the variable slot.
  178. Opening the inspector dropdown may result in an
  179. extremely long list of possible classes to create, however.
  180. Therefore, if you specify an extension of Resource such as:
  181. ::
  182. @export var resource: AnimationNode
  183. The drop-down menu will be limited to AnimationNode and all
  184. its derived classes.
  185. It must be noted that even if the script is not being run while in the
  186. editor, the exported properties are still editable. This can be used
  187. in conjunction with a :ref:`script in "tool" mode <doc_gdscript_tool_mode>`.
  188. .. _doc_gdscript_exports_exporting_bit_flags:
  189. Exporting bit flags
  190. -------------------
  191. See :ref:`@export_flags <class_@GDScript_annotation_@export_flags>`.
  192. Integers used as bit flags can store multiple ``true``/``false`` (boolean)
  193. values in one property. By using the ``@export_flags`` annotation, they
  194. can be set from the editor::
  195. # Set any of the given flags from the editor.
  196. @export_flags("Fire", "Water", "Earth", "Wind") var spell_elements = 0
  197. You must provide a string description for each flag. In this example, ``Fire``
  198. has value 1, ``Water`` has value 2, ``Earth`` has value 4 and ``Wind``
  199. corresponds to value 8. Usually, constants should be defined accordingly (e.g.
  200. ``const ELEMENT_WIND = 8`` and so on).
  201. You can add explicit values using a colon::
  202. @export_flags("Self:4", "Allies:8", "Foes:16") var spell_targets = 0
  203. Only power of 2 values are valid as bit flags options. The lowest allowed value
  204. is 1, as 0 means that nothing is selected. You can also add options that are a
  205. combination of other flags::
  206. @export_flags("Self:4", "Allies:8", "Self and Allies:12", "Foes:16")
  207. var spell_targets = 0
  208. Export annotations are also provided for the physics, render, and navigation layers defined in the project settings::
  209. @export_flags_2d_physics var layers_2d_physics
  210. @export_flags_2d_render var layers_2d_render
  211. @export_flags_2d_navigation var layers_2d_navigation
  212. @export_flags_3d_physics var layers_3d_physics
  213. @export_flags_3d_render var layers_3d_render
  214. @export_flags_3d_navigation var layers_3d_navigation
  215. Using bit flags requires some understanding of bitwise operations.
  216. If in doubt, use boolean variables instead.
  217. Exporting enums
  218. ---------------
  219. See :ref:`@export_enum <class_@GDScript_annotation_@export_enum>`.
  220. Properties can be exported with a type hint referencing an enum to limit their values
  221. to the values of the enumeration. The editor will create a widget in the Inspector, enumerating
  222. the following as "Thing 1", "Thing 2", "Another Thing". The value will be stored as an integer.
  223. ::
  224. enum NamedEnum {THING_1, THING_2, ANOTHER_THING = -1}
  225. @export var x: NamedEnum
  226. Integer and string properties can also be limited to a specific list of values using
  227. the :ref:`@export_enum <class_@GDScript_annotation_@export_enum>` annotation.
  228. The editor will create a widget in the Inspector, enumerating the following as Warrior,
  229. Magician, Thief. The value will be stored as an integer, corresponding to the index
  230. of the selected option (i.e. ``0``, ``1``, or ``2``).
  231. ::
  232. @export_enum("Warrior", "Magician", "Thief") var character_class: int
  233. You can add explicit values using a colon::
  234. @export_enum("Slow:30", "Average:60", "Very Fast:200") var character_speed: int
  235. If the type is String, the value will be stored as a string.
  236. ::
  237. @export_enum("Rebecca", "Mary", "Leah") var character_name: String
  238. If you want to set an initial value, you must specify it explicitly::
  239. @export_enum("Rebecca", "Mary", "Leah") var character_name: String = "Rebecca"
  240. Exporting arrays
  241. ----------------
  242. Exported arrays can have initializers, but they must be constant expressions.
  243. If the exported array specifies a type which inherits from Resource, the array
  244. values can be set in the inspector by dragging and dropping multiple files
  245. from the FileSystem dock at once.
  246. The default value **must** be a constant expression.
  247. ::
  248. @export var a = [1, 2, 3]
  249. .. UPDATE: Not supported yet. When nested typed arrays are supported, update
  250. .. the example.
  251. Exported arrays can specify type (using the same hints as before).
  252. ::
  253. @export var ints: Array[int] = [1, 2, 3]
  254. # Nested typed arrays such as `Array[Array[float]]` are not supported yet.
  255. @export var two_dimensional: Array[Array] = [[1.0, 2.0], [3.0, 4.0]]
  256. You can omit the default value, but it would then be ``null`` if not assigned.
  257. ::
  258. @export var b: Array
  259. @export var scenes: Array[PackedScene]
  260. Arrays with specified types which inherit from resource can be set by
  261. drag-and-dropping multiple files from the FileSystem dock.
  262. ::
  263. @export var textures: Array[Texture] = []
  264. @export var scenes: Array[PackedScene] = []
  265. Packed type arrays also work, but only initialized empty:
  266. ::
  267. @export var vector3s = PackedVector3Array()
  268. @export var strings = PackedStringArray()
  269. Other export variants can also be used when exporting arrays:
  270. ::
  271. @export_range(-360, 360, 0.001, "degrees") var laser_angles: Array[float] = []
  272. @export_file("*.json") var skill_trees: Array[String] = []
  273. @export_color_no_alpha var hair_colors = PackedColorArray()
  274. @export_enum("Espresso", "Mocha", "Latte", "Capuccino") var barista_suggestions: Array[String] = []
  275. ``@export_storage``
  276. -------------------
  277. See :ref:`@export_storage <class_@GDScript_annotation_@export_storage>`.
  278. By default, exporting a property has two effects:
  279. 1. makes the property stored in the scene/resource file (:ref:`PROPERTY_USAGE_STORAGE <class_@GlobalScope_constant_PROPERTY_USAGE_STORAGE>`);
  280. 2. adds a field to the Inspector (:ref:`PROPERTY_USAGE_EDITOR <class_@GlobalScope_constant_PROPERTY_USAGE_EDITOR>`).
  281. However, sometimes you may want to make a property serializable, but not display it
  282. in the editor to prevent unintentional changes and cluttering the interface.
  283. To do this you can use :ref:`@export_storage <class_@GDScript_annotation_@export_storage>`.
  284. This can be useful for :ref:`@tool <class_@GDScript_annotation_@tool>` scripts.
  285. Also the property value is copied when :ref:`Resource.duplicate() <class_Resource_method_duplicate>`
  286. or :ref:`Node.duplicate() <class_Node_method_duplicate>` is called, unlike non-exported variables.
  287. ::
  288. var a # Not stored in the file, not displayed in the editor.
  289. @export_storage var b # Stored in the file, not displayed in the editor.
  290. @export var c: int # Stored in the file, displayed in the editor.
  291. ``@export_custom``
  292. ------------------
  293. If you need more control than what's exposed with the built-in ``@export``
  294. annotations, you can use ``@export_custom`` instead. This allows defining any
  295. property hint, hint string and usage flags, with a syntax similar to the one
  296. used by the editor for built-in nodes.
  297. For example, this exposes the ``altitude`` property with no range limits but a
  298. ``m`` (meter) suffix defined:
  299. ::
  300. @export_custom(PROPERTY_HINT_NONE, "suffix:m") var altitude: float
  301. The above is normally not feasible with the standard ``@export_range`` syntax,
  302. since it requires defining a range.
  303. See the :ref:`class reference <class_@GDScript_annotation_@export_custom>`
  304. for a list of parameters and their allowed values.
  305. .. warning::
  306. When using ``@export_custom``, GDScript does not perform any validation on
  307. the syntax. Invalid syntax may have unexpected behavior in the inspector.
  308. ``@export_tool_button``
  309. -----------------------
  310. If you need to create a clickable inspector button, you can use ``@export_tool_button``.
  311. This exports a ``Callable`` property as a clickable button. When the button is pressed, the callable is called.
  312. Export a button with label ``"Hello"`` and icon ``"Callable"``. When you press it, it will print ``"Hello world!"``.
  313. ::
  314. @tool
  315. extends Node
  316. @export_tool_button("Hello", "Callable") var hello_action = hello
  317. func hello():
  318. print("Hello world!")
  319. Setting exported variables from a tool script
  320. ---------------------------------------------
  321. When changing an exported variable's value from a script in
  322. :ref:`doc_gdscript_tool_mode`, the value in the inspector won't be updated
  323. automatically. To update it, call
  324. :ref:`notify_property_list_changed() <class_Object_method_notify_property_list_changed>`
  325. after setting the exported variable's value.
  326. Advanced exports
  327. ----------------
  328. Not every type of export can be provided on the level of the language itself to
  329. avoid unnecessary design complexity. The following describes some more or less
  330. common exporting features which can be implemented with a low-level API.
  331. Before reading further, you should get familiar with the way properties are
  332. handled and how they can be customized with
  333. :ref:`_set() <class_Object_private_method__set>`,
  334. :ref:`_get() <class_Object_private_method__get>`, and
  335. :ref:`_get_property_list() <class_Object_private_method__get_property_list>` methods as
  336. described in :ref:`doc_accessing_data_or_logic_from_object`.
  337. .. seealso:: For binding properties using the above methods in C++, see
  338. :ref:`doc_binding_properties_using_set_get_property_list`.
  339. .. warning:: The script must operate in the ``@tool`` mode so the above methods
  340. can work from within the editor.