gdscript_exports.rst 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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`` keyword::
  10. extends Button
  11. export var number = 5 # Value will be saved and visible in the property editor.
  12. An exported variable must be initialized to a constant expression or have an
  13. export hint in the form of an argument to the ``export`` keyword (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.
  22. Examples
  23. --------
  24. ::
  25. # If the exported value assigns a constant or constant expression,
  26. # the type will be inferred and used in the editor.
  27. export var number = 5
  28. # Export can take a basic data type as an argument, which will be
  29. # used in the editor.
  30. export(int) var number
  31. # Export can also take a resource type to use as a hint.
  32. export(Texture) var character_face
  33. export(PackedScene) var scene_file
  34. # There are many resource types that can be used this way, try e.g.
  35. # the following to list them:
  36. export(Resource) var resource
  37. # Integers and strings hint enumerated values.
  38. # Editor will enumerate as 0, 1 and 2.
  39. export(int, "Warrior", "Magician", "Thief") var character_class
  40. # Editor will enumerate with string names.
  41. export(String, "Rebecca", "Mary", "Leah") var character_name
  42. # Named enum values
  43. # Editor will enumerate as THING_1, THING_2, ANOTHER_THING.
  44. enum NamedEnum {THING_1, THING_2, ANOTHER_THING = -1}
  45. export(NamedEnum) var x
  46. # Strings as paths
  47. # String is a path to a file.
  48. export(String, FILE) var f
  49. # String is a path to a directory.
  50. export(String, DIR) var f
  51. # String is a path to a file, custom filter provided as hint.
  52. export(String, FILE, "*.txt") var f
  53. # Using paths in the global filesystem is also possible,
  54. # but only in scripts in "tool" mode.
  55. # String is a path to a PNG file in the global filesystem.
  56. export(String, FILE, GLOBAL, "*.png") var tool_image
  57. # String is a path to a directory in the global filesystem.
  58. export(String, DIR, GLOBAL) var tool_dir
  59. # The MULTILINE setting tells the editor to show a large input
  60. # field for editing over multiple lines.
  61. export(String, MULTILINE) var text
  62. # Limiting editor input ranges
  63. # Allow integer values from 0 to 20.
  64. export(int, 20) var i
  65. # Allow integer values from -10 to 20.
  66. export(int, -10, 20) var j
  67. # Allow floats from -10 to 20 and snap the value to multiples of 0.2.
  68. export(float, -10, 20, 0.2) var k
  69. # Allow values 'y = exp(x)' where 'y' varies between 100 and 1000
  70. # while snapping to steps of 20. The editor will present a
  71. # slider for easily editing the value.
  72. export(float, EXP, 100, 1000, 20) var l
  73. # Floats with easing hint
  74. # Display a visual representation of the 'ease()' function
  75. # when editing.
  76. export(float, EASE) var transition_speed
  77. # Colors
  78. # Color given as red-green-blue value (alpha will always be 1).
  79. export(Color, RGB) var col
  80. # Color given as red-green-blue-alpha value.
  81. export(Color, RGBA) var col
  82. # Nodes
  83. # Another node in the scene can be exported as a NodePath.
  84. export(NodePath) var node_path
  85. # Do take note that the node itself isn't being exported -
  86. # there is one more step to call the true node:
  87. var node = get_node(node_path)
  88. # Resources
  89. export(Resource) var resource
  90. # In the Inspector, you can then drag and drop a resource file
  91. # from the FileSystem dock into the variable slot.
  92. # Opening the inspector dropdown may result in an
  93. # extremely long list of possible classes to create, however.
  94. # Therefore, if you specify an extension of Resource such as:
  95. export(AnimationNode) var resource
  96. # The drop-down menu will be limited to AnimationNode and all
  97. # its inherited classes.
  98. It must be noted that even if the script is not being run while in the
  99. editor, the exported properties are still editable. This can be used
  100. in conjunction with a :ref:`script in "tool" mode <doc_gdscript_tool_mode>`.
  101. Exporting bit flags
  102. -------------------
  103. Integers used as bit flags can store multiple ``true``/``false`` (boolean)
  104. values in one property. By using the export hint ``int, FLAGS, ...``, they
  105. can be set from the editor::
  106. # Set any of the given flags from the editor.
  107. export(int, FLAGS, "Fire", "Water", "Earth", "Wind") var spell_elements = 0
  108. You must provide a string description for each flag. In this example, ``Fire``
  109. has value 1, ``Water`` has value 2, ``Earth`` has value 4 and ``Wind``
  110. corresponds to value 8. Usually, constants should be defined accordingly (e.g.
  111. ``const ELEMENT_WIND = 8`` and so on).
  112. Export hints are also provided for the physics and render layers defined in the project settings::
  113. export(int, LAYERS_2D_PHYSICS) var layers_2d_physics
  114. export(int, LAYERS_2D_RENDER) var layers_2d_render
  115. export(int, LAYERS_3D_PHYSICS) var layers_3d_physics
  116. export(int, LAYERS_3D_RENDER) var layers_3d_render
  117. Using bit flags requires some understanding of bitwise operations.
  118. If in doubt, use boolean variables instead.
  119. Exporting arrays
  120. ----------------
  121. Exported arrays can have initializers, but they must be constant expressions.
  122. If the exported array specifies a type which inherits from Resource, the array
  123. values can be set in the inspector by dragging and dropping multiple files
  124. from the FileSystem dock at once.
  125. ::
  126. # Default value must be a constant expression.
  127. export var a = [1, 2, 3]
  128. # Exported arrays can specify type (using the same hints as before).
  129. export(Array, int) var ints = [1, 2, 3]
  130. export(Array, int, "Red", "Green", "Blue") var enums = [2, 1, 0]
  131. export(Array, Array, float) var two_dimensional = [[1.0, 2.0], [3.0, 4.0]]
  132. # You can omit the default value, but then it would be null if not assigned.
  133. export(Array) var b
  134. export(Array, PackedScene) var scenes
  135. # Arrays with specified types which inherit from resource can be set by
  136. # drag-and-dropping multiple files from the FileSystem dock.
  137. export(Array, Texture) var textures
  138. export(Array, PackedScene) var scenes
  139. # Typed arrays also work, only initialized empty:
  140. export var vector3s = PoolVector3Array()
  141. export var strings = PoolStringArray()
  142. # Default value can include run-time values, but can't
  143. # be exported.
  144. var c = [a, 2, 3]
  145. Setting exported variables from a tool script
  146. ---------------------------------------------
  147. When changing an exported variable's value from a script in
  148. :ref:`doc_gdscript_tool_mode`, the value in the inspector won't be updated
  149. automatically. To update it, call
  150. :ref:`property_list_changed_notify() <class_Object_method_property_list_changed_notify>`
  151. after setting the exported variable's value.
  152. Advanced exports
  153. ----------------
  154. Not every type of export can be provided on the level of the language itself to
  155. avoid unnecessary design complexity. The following describes some more or less
  156. common exporting features which can be implemented with a low-level API.
  157. Before reading further, you should get familiar with the way properties are
  158. handled and how they can be customized with
  159. :ref:`_set() <class_Object_method__get_property_list>`,
  160. :ref:`_get() <class_Object_method__get_property_list>`, and
  161. :ref:`_get_property_list() <class_Object_method__get_property_list>` methods as
  162. described in :ref:`doc_accessing_data_or_logic_from_object`.
  163. .. seealso:: For binding properties using the above methods in C++, see
  164. :ref:`doc_binding_properties_using_set_get_property_list`.
  165. .. warning:: The script must operate in the ``tool`` mode so the above methods
  166. can work from within the editor.
  167. Adding script categories
  168. ~~~~~~~~~~~~~~~~~~~~~~~~
  169. For better visual distinguishing of properties, a special script category can be
  170. embedded into the inspector to act as a separator. ``Script Variables`` is one
  171. example of a built-in category.
  172. ::
  173. func _get_property_list():
  174. var properties = []
  175. properties.append(
  176. {
  177. name = "Debug",
  178. type = TYPE_NIL,
  179. usage = PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_SCRIPT_VARIABLE
  180. }
  181. )
  182. return properties
  183. * ``name`` is the name of a category to be added to the inspector;
  184. * ``PROPERTY_USAGE_CATEGORY`` indicates that the property should be treated as a
  185. script category specifically, so the type ``TYPE_NIL`` can be ignored as it
  186. won't be actually used for the scripting logic, yet it must be defined anyway.
  187. Grouping properties
  188. ~~~~~~~~~~~~~~~~~~~
  189. A list of properties with similar names can be grouped.
  190. ::
  191. func _get_property_list():
  192. var properties = []
  193. properties.append({
  194. name = "Rotate",
  195. type = TYPE_NIL,
  196. hint_string = "rotate_",
  197. usage = PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SCRIPT_VARIABLE
  198. })
  199. return properties
  200. * ``name`` is the name of a group which is going to be displayed as collapsible
  201. list of properties;
  202. * every successive property added after the group property will be collapsed and
  203. shortened as determined by the prefix defined via the ``hint_string`` key. For
  204. instance, ``rotate_speed`` is going to be shortened to ``speed`` in this case.
  205. * ``PROPERTY_USAGE_GROUP`` indicates that the property should be treated as a
  206. script group specifically, so the type ``TYPE_NIL`` can be ignored as it
  207. won't be actually used for the scripting logic, yet it must be defined anyway.