Tween.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="Tween" inherits="RefCounted" version="4.0">
  3. <brief_description>
  4. Lightweight object used for general-purpose animation via script, using [Tweener]s.
  5. </brief_description>
  6. <description>
  7. Tweens are mostly useful for animations requiring a numerical property to be interpolated over a range of values. The name [i]tween[/i] comes from [i]in-betweening[/i], an animation technique where you specify [i]keyframes[/i] and the computer interpolates the frames that appear between them.
  8. [Tween] is more suited than [AnimationPlayer] for animations where you don't know the final values in advance. For example, interpolating a dynamically-chosen camera zoom value is best done with a [Tween]; it would be difficult to do the same thing with an [AnimationPlayer] node. Tweens are also more light-weight than [AnimationPlayer], so they are very much suited for simple animations or general tasks that don't require visual tweaking provided by the editor. They can be used in a fire-and-forget manner for some logic that normally would be done by code. You can e.g. make something shoot periodically by using a looped [CallbackTweener] with a delay.
  9. A [Tween] can be created by using either [method SceneTree.create_tween] or [method Node.create_tween]. [Tween]s created manually (i.e. by using [code]Tween.new()[/code]) are invalid. They can't be used for tweening values, but you can do manual interpolation with [method interpolate_value].
  10. A [Tween] animation is composed of a sequence of [Tweener]s, which by default are executed one after another. You can create a sequence by appending [Tweener]s to the [Tween]. Animating something with a [Tweener] is called tweening. Example tweening sequence looks like this:
  11. [codeblock]
  12. var tween = get_tree().create_tween()
  13. tween.tween_property($Sprite, "modulate", Color.red, 1)
  14. tween.tween_property($Sprite, "scale", Vector2(), 1)
  15. tween.tween_callback($Sprite.queue_free)
  16. [/codeblock]
  17. This sequence will make the [code]$Sprite[/code] node turn red, then shrink and finally the [method Node.queue_free] is called to remove the sprite. See methods [method tween_property], [method tween_interval], [method tween_callback] and [method tween_method] for more usage information.
  18. When a [Tweener] is created with one of the [code]tween_*[/code] methods, a chained method call can be used to tweak the properties of this [Tweener]. For example, if you want to set different transition type in the above example, you can do:
  19. [codeblock]
  20. var tween = get_tree().create_tween()
  21. tween.tween_property($Sprite, "modulate", Color.red, 1).set_trans(Tween.TRANS_SINE)
  22. tween.tween_property($Sprite, "scale", Vector2(), 1).set_trans(Tween.TRANS_BOUNCE)
  23. tween.tween_callback($Sprite.queue_free)
  24. [/codeblock]
  25. Most of the [Tween] methods can be chained this way too. In this example the [Tween] is bound and have set a default transition:
  26. [codeblock]
  27. var tween = get_tree().create_tween().bind_node(self).set_trans(Tween.TRANS_ELASTIC)
  28. tween.tween_property($Sprite, "modulate", Color.red, 1)
  29. tween.tween_property($Sprite, "scale", Vector2(), 1)
  30. tween.tween_callback($Sprite.queue_free)
  31. [/codeblock]
  32. Another interesting use for [Tween]s is animating arbitrary set of objects:
  33. [codeblock]
  34. var tween = create_tween()
  35. for sprite in get_children():
  36. tween.tween_property(sprite, "position", Vector2(), 1)
  37. [/codeblock]
  38. In the example above, all children of a node are moved one after another to position (0, 0).
  39. Some [Tweener]s use transitions and eases. The first accepts an [enum TransitionType] constant, and refers to the way the timing of the animation is handled (see [url=https://easings.net/]easings.net[/url] for some examples). The second accepts an [enum EaseType] constant, and controls where the [code]trans_type[/code] is applied to the interpolation (in the beginning, the end, or both). If you don't know which transition and easing to pick, you can try different [enum TransitionType] constants with [constant EASE_IN_OUT], and use the one that looks best.
  40. [url=https://raw.githubusercontent.com/godotengine/godot-docs/master/img/tween_cheatsheet.png]Tween easing and transition types cheatsheet[/url]
  41. [b]Note:[/b] All [Tween]s will automatically start by default. To prevent a [Tween] from autostarting, you can call [method stop] immediately after it was created.
  42. </description>
  43. <tutorials>
  44. </tutorials>
  45. <methods>
  46. <method name="bind_node">
  47. <return type="Tween" />
  48. <argument index="0" name="node" type="Node" />
  49. <description>
  50. Binds this [Tween] with the given [code]node[/code]. [Tween]s are processed directly by the [SceneTree], so they run independently of the animated nodes. When you bind a [Node] with the [Tween], the [Tween] will halt the animation when the object is not inside tree and the [Tween] will be automatically killed when the bound object is freed. Also [constant TWEEN_PAUSE_BOUND] will make the pausing behavior dependent on the bound node.
  51. For a shorter way to create and bind a [Tween], you can use [method Node.create_tween].
  52. </description>
  53. </method>
  54. <method name="chain">
  55. <return type="Tween" />
  56. <description>
  57. Used to chain two [Tweener]s after [method set_parallel] is called with [code]true[/code].
  58. [codeblock]
  59. var tween = create_tween().set_parallel(true)
  60. tween.tween_property(...)
  61. tween.tween_property(...) # Will run parallelly with above.
  62. tween.chain().tween_property(...) # Will run after two above are finished.
  63. [/codeblock]
  64. </description>
  65. </method>
  66. <method name="custom_step">
  67. <return type="bool" />
  68. <argument index="0" name="delta" type="float" />
  69. <description>
  70. Processes the [Tween] by given [code]delta[/code] value, in seconds. Mostly useful when the [Tween] is paused, for controlling it manually. Can also be used to end the [Tween] animation immediately, by using [code]delta[/code] longer than the whole duration.
  71. Returns [code]true[/code] if the [Tween] still has [Tweener]s that haven't finished.
  72. [b]Note:[/b] The [Tween] will become invalid after finished, but you can call [method stop] after the step, to keep it and reset.
  73. </description>
  74. </method>
  75. <method name="interpolate_value">
  76. <return type="Variant" />
  77. <argument index="0" name="initial_value" type="Variant" />
  78. <argument index="1" name="delta_value" type="Variant" />
  79. <argument index="2" name="elapsed_time" type="float" />
  80. <argument index="3" name="duration" type="float" />
  81. <argument index="4" name="trans_type" type="int" enum="Tween.TransitionType" />
  82. <argument index="5" name="ease_type" type="int" enum="Tween.EaseType" />
  83. <description>
  84. This method can be used for manual interpolation of a value, when you don't want [Tween] to do animating for you. It's similar to [method @GlobalScope.lerp], but with support for custom transition and easing.
  85. [code]initial_value[/code] is the starting value of the interpolation.
  86. [code]delta_value[/code] is the change of the value in the interpolation, i.e. it's equal to [code]final_value - initial_value[/code].
  87. [code]elapsed_time[/code] is the time in seconds that passed after the interpolation started and it's used to control the position of the interpolation. E.g. when it's equal to half of the [code]duration[/code], the interpolated value will be halfway between initial and final values. This value can also be greater than [code]duration[/code] or lower than 0, which will extrapolate the value.
  88. [code]duration[/code] is the total time of the interpolation.
  89. [b]Note:[/b] If [code]duration[/code] is equal to [code]0[/code], the method will always return the final value, regardless of [code]elapsed_time[/code] provided.
  90. </description>
  91. </method>
  92. <method name="is_running">
  93. <return type="bool" />
  94. <description>
  95. Returns whether the [Tween] is currently running, i.e. it wasn't paused and it's not finished.
  96. </description>
  97. </method>
  98. <method name="is_valid">
  99. <return type="bool" />
  100. <description>
  101. Returns whether the [Tween] is valid. A valid [Tween] is a [Tween] contained by the scene tree (i.e. the array from [method SceneTree.get_processed_tweens] will contain this [Tween]). [Tween] might become invalid when it has finished tweening or was killed, also when created with [code]Tween.new()[/code]. Invalid [Tween] can't have [Tweener]s appended, because it can't animate them. You can however still use [method interpolate_value].
  102. </description>
  103. </method>
  104. <method name="kill">
  105. <return type="void" />
  106. <description>
  107. Aborts all tweening operations and invalidates the [Tween].
  108. </description>
  109. </method>
  110. <method name="parallel">
  111. <return type="Tween" />
  112. <description>
  113. Makes the next [Tweener] run parallelly to the previous one. Example:
  114. [codeblock]
  115. var tween = create_tween()
  116. tween.tween_property(...)
  117. tween.parallel().tween_property(...)
  118. tween.parallel().tween_property(...)
  119. [/codeblock]
  120. All [Tweener]s in the example will run at the same time.
  121. You can make the [Tween] parallel by default by using [method set_parallel].
  122. </description>
  123. </method>
  124. <method name="pause">
  125. <return type="void" />
  126. <description>
  127. Pauses the tweening. The animation can be resumed by using [method play].
  128. </description>
  129. </method>
  130. <method name="play">
  131. <return type="void" />
  132. <description>
  133. Resumes a paused or stopped [Tween].
  134. </description>
  135. </method>
  136. <method name="set_ease">
  137. <return type="Tween" />
  138. <argument index="0" name="ease" type="int" enum="Tween.EaseType" />
  139. <description>
  140. Sets the default ease type for [PropertyTweener]s and [MethodTweener]s animated by this [Tween].
  141. </description>
  142. </method>
  143. <method name="set_loops">
  144. <return type="Tween" />
  145. <argument index="0" name="loops" type="int" default="0" />
  146. <description>
  147. Sets the number of times the tweening sequence will be repeated, i.e. [code]set_loops(2)[/code] will run the animation twice.
  148. Calling this method without arguments will make the [Tween] run infinitely, until it is either killed by [method kill] or by freeing bound node, or all the animated objects have been freed (which makes further animation impossible).
  149. [b]Warning:[/b] Make sure to always add some duration/delay when using infinite loops. 0-duration looped animations (e.g. single [CallbackTweener] with no delay) are equivalent to infinite [code]while[/code] loops and will freeze your game.
  150. </description>
  151. </method>
  152. <method name="set_parallel">
  153. <return type="Tween" />
  154. <argument index="0" name="parallel" type="bool" default="true" />
  155. <description>
  156. If [code]parallel[/code] is [code]true[/code], the [Tweener]s appended after this method will by default run simultaneously, as opposed to sequentially.
  157. </description>
  158. </method>
  159. <method name="set_pause_mode">
  160. <return type="Tween" />
  161. <argument index="0" name="mode" type="int" enum="Tween.TweenPauseMode" />
  162. <description>
  163. Determines the behavior of the [Tween] when the [SceneTree] is paused. Check [enum TweenPauseMode] for options.
  164. Default value is [constant TWEEN_PAUSE_BOUND].
  165. </description>
  166. </method>
  167. <method name="set_process_mode">
  168. <return type="Tween" />
  169. <argument index="0" name="mode" type="int" enum="Tween.TweenProcessMode" />
  170. <description>
  171. Determines whether the [Tween] should run during idle frame (see [method Node._process]) or physics frame (see [method Node._physics_process].
  172. Default value is [constant TWEEN_PROCESS_IDLE].
  173. </description>
  174. </method>
  175. <method name="set_speed_scale">
  176. <return type="Tween" />
  177. <argument index="0" name="speed" type="float" />
  178. <description>
  179. Scales the speed of tweening. This affects all [Tweener]s and their delays.
  180. </description>
  181. </method>
  182. <method name="set_trans">
  183. <return type="Tween" />
  184. <argument index="0" name="trans" type="int" enum="Tween.TransitionType" />
  185. <description>
  186. Sets the default transition type for [PropertyTweener]s and [MethodTweener]s animated by this [Tween].
  187. </description>
  188. </method>
  189. <method name="stop">
  190. <return type="void" />
  191. <description>
  192. Stops the tweening and resets the [Tween] to its initial state. This will not remove any appended [Tweener]s.
  193. </description>
  194. </method>
  195. <method name="tween_callback">
  196. <return type="CallbackTweener" />
  197. <argument index="0" name="callback" type="Callable" />
  198. <description>
  199. Creates and appends a [CallbackTweener]. This method can be used to call an arbitrary method in any object. Use [method Callable.bind] to bind additional arguments for the call.
  200. Example: object that keeps shooting every 1 second.
  201. [codeblock]
  202. var tween = get_tree().create_tween().set_loops()
  203. tween.tween_callback(shoot).set_delay(1)
  204. [/codeblock]
  205. Example: turning a sprite red and then blue, with 2 second delay.
  206. [codeblock]
  207. var tween = get_tree().create_tween()
  208. tween.tween_callback($Sprite.set_modulate.bind(Color.red)).set_delay(2)
  209. tween.tween_callback($Sprite.set_modulate.bind(Color.blue)).set_delay(2)
  210. [/codeblock]
  211. </description>
  212. </method>
  213. <method name="tween_interval">
  214. <return type="IntervalTweener" />
  215. <argument index="0" name="time" type="float" />
  216. <description>
  217. Creates and appends an [IntervalTweener]. This method can be used to create delays in the tween animation, as an alternative for using the delay in other [Tweener]s or when there's no animation (in which case the [Tween] acts as a timer). [code]time[/code] is the length of the interval, in seconds.
  218. Example: creating an interval in code execution.
  219. [codeblock]
  220. # ... some code
  221. await create_tween().tween_interval(2).finished
  222. # ... more code
  223. [/codeblock]
  224. Example: creating an object that moves back and forth and jumps every few seconds.
  225. [codeblock]
  226. var tween = create_tween().set_loops()
  227. tween.tween_property("position:x", 200, 1).as_relative()
  228. tween.tween_callback(jump)
  229. tween.tween_interval(2)
  230. tween.tween_property("position:x", -200, 1).as_relative()
  231. tween.tween_callback(jump)
  232. tween.tween_interval(2)
  233. [/codeblock]
  234. </description>
  235. </method>
  236. <method name="tween_method">
  237. <return type="MethodTweener" />
  238. <argument index="0" name="method" type="Callable" />
  239. <argument index="1" name="from" type="Variant" />
  240. <argument index="2" name="to" type="Variant" />
  241. <argument index="3" name="duration" type="float" />
  242. <description>
  243. Creates and appends a [MethodTweener]. This method is similar to a combination of [method tween_callback] and [method tween_property]. It calls a method over time with a tweened value provided as an argument. The value is tweened between [code]from[/code] and [code]to[/code] over the time specified by [code]duration[/code], in seconds. Use [method Callable.bind] to bind additional arguments for the call. You can use [method MethodTweener.set_ease] and [method MethodTweener.set_trans] to tweak the easing and transition of the value or [method MethodTweener.set_delay] to delay the tweening.
  244. Example: making a 3D object look from one point to another point.
  245. [codeblock]
  246. var tween = create_tween()
  247. tween.tween_method(look_at.bind(Vector3.UP), Vector3(-1, 0, -1), Vector3(1, 0, -1), 1) # The look_at() method takes up vector as second argument.
  248. [/codeblock]
  249. Example: setting a text of a [Label], using an intermediate method and after a delay.
  250. [codeblock]
  251. func _ready():
  252. var tween = create_tween()
  253. tween.tween_method(set_label_text, 0, 10, 1).set_delay(1)
  254. func set_label_text(value: int):
  255. $Label.text = "Counting " + str(value)
  256. [/codeblock]
  257. </description>
  258. </method>
  259. <method name="tween_property">
  260. <return type="PropertyTweener" />
  261. <argument index="0" name="object" type="Object" />
  262. <argument index="1" name="property" type="NodePath" />
  263. <argument index="2" name="final_val" type="Variant" />
  264. <argument index="3" name="duration" type="float" />
  265. <description>
  266. Creates and appends a [PropertyTweener]. This method tweens a [code]property[/code] of an [code]object[/code] between an initial value and [code]final_val[/code] in a span of time equal to [code]duration[/code], in seconds. The initial value by default is a value at the time the tweening of the [PropertyTweener] start. For example:
  267. [codeblock]
  268. var tween = create_tween()
  269. tween.tween_property($Sprite, "position", Vector2(100, 200)
  270. tween.tween_property($Sprite, "position", Vector2(200, 300)
  271. [/codeblock]
  272. will move the sprite to position (100, 200) and then to (200, 300). If you use [method PropertyTweener.from] or [method PropertyTweener.from_current], the starting position will be overwritten by the given value instead. See other methods in [PropertyTweener] to see how the tweening can be tweaked further.
  273. [b]Note:[/b] You can find the correct property name by hovering over the property in the Inspector. You can also provide the components of a property directly by using [code]"property:component"[/code] (eg. [code]position:x[/code]), where it would only apply to that particular component.
  274. Example: moving object twice from the same position, with different transition types.
  275. [codeblock]
  276. var tween = create_tween()
  277. tween.tween_property($Sprite, "position", Vector2.RIGHT * 300).as_relative().set_trans(Tween.TRANS_SINE)
  278. tween.tween_property($Sprite, "position", Vector2.RIGHT * 300).as_relative().from_current().set_trans(Tween.TRANS_EXPO)
  279. [/codeblock]
  280. </description>
  281. </method>
  282. </methods>
  283. <signals>
  284. <signal name="finished">
  285. <description>
  286. Emitted when the [Tween] has finished all tweening. Never emitted when the [Tween] is set to infinite looping (see [method set_loops]).
  287. [b]Note:[/b] The [Tween] is removed (invalidated) after this signal is emitted, but it doesn't happen immediately, but on the next processing frame. Calling [method stop] inside the signal callback will preserve the [Tween].
  288. </description>
  289. </signal>
  290. <signal name="loop_finished">
  291. <argument index="0" name="loop_count" type="int" />
  292. <description>
  293. Emitted when a full loop is complete (see [method set_loops]), providing the loop index. This signal is not emitted after final loop, use [signal finished] instead for this case.
  294. </description>
  295. </signal>
  296. <signal name="step_finished">
  297. <argument index="0" name="idx" type="int" />
  298. <description>
  299. Emitted when one step of the [Tween] is complete, providing the step index. One step is either a single [Tweener] or a group of [Tweener]s running parallelly.
  300. </description>
  301. </signal>
  302. </signals>
  303. <constants>
  304. <constant name="TWEEN_PROCESS_PHYSICS" value="0" enum="TweenProcessMode">
  305. The [Tween] updates during physics frame.
  306. </constant>
  307. <constant name="TWEEN_PROCESS_IDLE" value="1" enum="TweenProcessMode">
  308. The [Tween] updates during idle
  309. </constant>
  310. <constant name="TWEEN_PAUSE_BOUND" value="0" enum="TweenPauseMode">
  311. </constant>
  312. <constant name="TWEEN_PAUSE_STOP" value="1" enum="TweenPauseMode">
  313. </constant>
  314. <constant name="TWEEN_PAUSE_PROCESS" value="2" enum="TweenPauseMode">
  315. </constant>
  316. <constant name="TRANS_LINEAR" value="0" enum="TransitionType">
  317. </constant>
  318. <constant name="TRANS_SINE" value="1" enum="TransitionType">
  319. </constant>
  320. <constant name="TRANS_QUINT" value="2" enum="TransitionType">
  321. </constant>
  322. <constant name="TRANS_QUART" value="3" enum="TransitionType">
  323. </constant>
  324. <constant name="TRANS_QUAD" value="4" enum="TransitionType">
  325. </constant>
  326. <constant name="TRANS_EXPO" value="5" enum="TransitionType">
  327. </constant>
  328. <constant name="TRANS_ELASTIC" value="6" enum="TransitionType">
  329. </constant>
  330. <constant name="TRANS_CUBIC" value="7" enum="TransitionType">
  331. </constant>
  332. <constant name="TRANS_CIRC" value="8" enum="TransitionType">
  333. </constant>
  334. <constant name="TRANS_BOUNCE" value="9" enum="TransitionType">
  335. </constant>
  336. <constant name="TRANS_BACK" value="10" enum="TransitionType">
  337. </constant>
  338. <constant name="EASE_IN" value="0" enum="EaseType">
  339. </constant>
  340. <constant name="EASE_OUT" value="1" enum="EaseType">
  341. </constant>
  342. <constant name="EASE_IN_OUT" value="2" enum="EaseType">
  343. </constant>
  344. <constant name="EASE_OUT_IN" value="3" enum="EaseType">
  345. </constant>
  346. </constants>
  347. </class>