summaryrefslogtreecommitdiff
path: root/doc/classes/Tween.xml
blob: ed193b9f7e3e5335c334ceca9da00e95d551e4a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?xml version="1.0" encoding="UTF-8" ?>
<class name="Tween" inherits="RefCounted" version="4.0">
	<brief_description>
		Lightweight object used for general-purpose animation via script, using [Tweener]s.
	</brief_description>
	<description>
		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.
		[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.
		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].
		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:
		[codeblock]
		var tween = get_tree().create_tween()
		tween.tween_property($Sprite, "modulate", Color.red, 1)
		tween.tween_property($Sprite, "scale", Vector2(), 1)
		tween.tween_callback($Sprite.queue_free)
		[/codeblock]
		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.
		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:
		[codeblock]
		var tween = get_tree().create_tween()
		tween.tween_property($Sprite, "modulate", Color.red, 1).set_trans(Tween.TRANS_SINE)
		tween.tween_property($Sprite, "scale", Vector2(), 1).set_trans(Tween.TRANS_BOUNCE)
		tween.tween_callback($Sprite.queue_free)
		[/codeblock]
		Most of the [Tween] methods can be chained this way too. In this example the [Tween] is bound and have set a default transition:
		[codeblock]
		var tween = get_tree().create_tween().bind_node(self).set_trans(Tween.TRANS_ELASTIC)
		tween.tween_property($Sprite, "modulate", Color.red, 1)
		tween.tween_property($Sprite, "scale", Vector2(), 1)
		tween.tween_callback($Sprite.queue_free)
		[/codeblock]
		Another interesting use for [Tween]s is animating arbitrary set of objects:
		[codeblock]
		var tween = create_tween()
		for sprite in get_children():
		    tween.tween_property(sprite, "position", Vector2(), 1)
		[/codeblock]
		In the example above, all children of a node are moved one after another to position (0, 0).
		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.
		[url=https://raw.githubusercontent.com/godotengine/godot-docs/master/img/tween_cheatsheet.png]Tween easing and transition types cheatsheet[/url]
		[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.
	</description>
	<tutorials>
	</tutorials>
	<methods>
		<method name="bind_node">
			<return type="Tween">
			</return>
			<argument index="0" name="node" type="Node">
			</argument>
			<description>
				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.
				For a shorter way to create and bind a [Tween], you can use [method Node.create_tween].
			</description>
		</method>
		<method name="chain">
			<return type="Tween">
			</return>
			<description>
				Used to chain two [Tweener]s after [method set_parallel] is called with [code]true[/code].
				[codeblock]
				var tween = create_tween().set_parallel(true)
				tween.tween_property(...)
				tween.tween_property(...) #will run parallelly with above
				tween.chain().tween_property(...) #will run after two above are finished
				[/codeblock]
			</description>
		</method>
		<method name="custom_step">
			<return type="bool">
			</return>
			<argument index="0" name="delta" type="float">
			</argument>
			<description>
				Processes the [Tween] by given [code]delta[/code] value, in seconds. Mostly useful when the [Tween] is paused, for controlling it manually.
				Returns [code]true[/code] if the [Tween] still has [Tweener]s that haven't finished.
				[b]Note:[/b] The [Tween] will become invalid after finished, but you can call [method stop] after the step, to keep it and reset.
				[b]Note:[/b] [method custom_step] will process only one step of the [Tween]. If the [code]delta[/code] is greater than the remaining time, the excessive time will not have any effect.
			</description>
		</method>
		<method name="interpolate_value">
			<return type="Variant">
			</return>
			<argument index="0" name="trans_type" type="Variant">
			</argument>
			<argument index="1" name="ease_type" type="Variant">
			</argument>
			<argument index="2" name="elapsed_time" type="float">
			</argument>
			<argument index="3" name="initial_value" type="float">
			</argument>
			<argument index="4" name="delta_value" type="int" enum="Tween.TransitionType">
			</argument>
			<argument index="5" name="duration" type="int" enum="Tween.EaseType">
			</argument>
			<description>
				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.
				[code]elapsed_time[/code] is the time in seconds that passed after the interping 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.
				[code]initial_value[/code] is the starting value of the interpolation.
				[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].
				[code]duration[/code] is the total time of the interpolation.
			</description>
		</method>
		<method name="is_running">
			<return type="bool">
			</return>
			<description>
				Returns whether the [Tween] is currently running, i.e. it wasn't paused and it's not finished.
			</description>
		</method>
		<method name="is_valid">
			<return type="bool">
			</return>
			<description>
				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].
			</description>
		</method>
		<method name="kill">
			<return type="void">
			</return>
			<description>
				Aborts all tweening operations and invalidates the [Tween].
			</description>
		</method>
		<method name="parallel">
			<return type="Tween">
			</return>
			<description>
				Makes the next [Tweener] run parallely to the previous one. Example:
				[codeblock]
				var tween = create_tween()
				tween.tween_property(...)
				tween.parallel().tween_property(...)
				tween.parallel().tween_property(...)
				[/codeblock]
				All [Tweener]s in the example will run at the same time.
				You can make the [Tween] parallel by default by using [method set_parallel].
			</description>
		</method>
		<method name="pause">
			<return type="void">
			</return>
			<description>
				Pauses the tweening. The animation can be resumed by using [method play].
			</description>
		</method>
		<method name="play">
			<return type="void">
			</return>
			<description>
				Resumes a paused or stopped [Tween].
			</description>
		</method>
		<method name="set_ease">
			<return type="Tween">
			</return>
			<argument index="0" name="ease" type="int" enum="Tween.EaseType">
			</argument>
			<description>
				Sets the default ease type for [PropertyTweener]s and [MethodTweener]s animated by this [Tween].
			</description>
		</method>
		<method name="set_loops">
			<return type="Tween">
			</return>
			<argument index="0" name="loops" type="int" default="0">
			</argument>
			<description>
				Sets the number of times the tweening sequence will be repeated, i.e. [code]set_loops(2)[/code] will run the animation twice.
				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).
			</description>
		</method>
		<method name="set_parallel">
			<return type="Tween">
			</return>
			<argument index="0" name="parallel" type="bool" default="true">
			</argument>
			<description>
				If [code]parallel[/code] is [code]true[/code], the [Tweener]s appended after this method will by default run simultanously, as opposed to sequentially.
			</description>
		</method>
		<method name="set_pause_mode">
			<return type="Tween">
			</return>
			<argument index="0" name="mode" type="int" enum="Tween.TweenPauseMode">
			</argument>
			<description>
				Determines the behavior of the [Tween] when the [SceneTree] is paused. Check [enum TweenPauseMode] for options.
				Default value is [constant TWEEN_PAUSE_BOUND].
			</description>
		</method>
		<method name="set_process_mode">
			<return type="Tween">
			</return>
			<argument index="0" name="mode" type="int" enum="Tween.TweenProcessMode">
			</argument>
			<description>
				Determines whether the [Tween] should run during idle frame (see [method Node._process]) or physics frame (see [method Node._physics_process].
				Default value is [constant TWEEN_PROCESS_IDLE].
			</description>
		</method>
		<method name="set_speed_scale">
			<return type="Tween">
			</return>
			<argument index="0" name="speed" type="float">
			</argument>
			<description>
				Scales the speed of tweening. This affects all [Tweener]s and their delays.
			</description>
		</method>
		<method name="set_trans">
			<return type="Tween">
			</return>
			<argument index="0" name="trans" type="int" enum="Tween.TransitionType">
			</argument>
			<description>
				Sets the default transition type for [PropertyTweener]s and [MethodTweener]s animated by this [Tween].
			</description>
		</method>
		<method name="stop">
			<return type="void">
			</return>
			<description>
				Stops the tweening and resets the [Tween] to its initial state. This will not remove any appended [Tweener]s.
			</description>
		</method>
		<method name="tween_callback">
			<return type="CallbackTweener">
			</return>
			<argument index="0" name="callback" type="Callable">
			</argument>
			<description>
				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.
				Example: object that keeps shooting every 1 second.
				[codeblock]
				var tween = get_tree().create_tween().set_loops()
				tween.tween_callback(shoot).set_delay(1)
				[/codeblock]
				Example: turning a sprite red and then blue, with 2 second delay.
				[codeblock]
				var tween = get_tree().create_tween()
				tween.tween_callback($Sprite.set_modulate.bind(Color.red)).set_delay(2)
				tween.tween_callback($Sprite.set_modulate.bind(Color.blue)).set_delay(2)
				[/codeblock]
			</description>
		</method>
		<method name="tween_interval">
			<return type="IntervalTweener">
			</return>
			<argument index="0" name="time" type="float">
			</argument>
			<description>
				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.
				Example: creating an interval in code execution.
				[codeblock]
				#... some code
				var tween = create_tween()
				tween.tween_interval(2)
				await tween.finished
				#... more code
				[/codeblock]
				Example: creating an object that moves back and forth and jumps every few seconds.
				[codeblock]
				var tween = create_tween().set_loops()
				tween.tween_property("position:x", 200, 1).as_relative()
				tween.tween_callback(jump)
				tween.tween_interval(2)
				tween.tween_property("position:x", -200, 1).as_relative()
				tween.tween_callback(jump)
				tween.tween_interval(2)
				[/codeblock]
			</description>
		</method>
		<method name="tween_method">
			<return type="MethodTweener">
			</return>
			<argument index="0" name="method" type="Callable">
			</argument>
			<argument index="1" name="from" type="float">
			</argument>
			<argument index="2" name="to" type="float">
			</argument>
			<argument index="3" name="duration" type="float">
			</argument>
			<description>
				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.
				Example: making a 3D object look from one point to another point.
				[codeblock]
				var tween = create_tween()
				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
				[/codeblock]
				Example: setting a text of a [Label], using an intermediate method and after a delay.
				[codeblock]
				func _ready():
				    var tween = create_tween()
				    tween.tween_method(set_label_text, 0, 10, 1).set_delay(1)

				func set_label_text(value: int):
				    $Label.text = "Counting " + str(value)
				[/codeblock]
			</description>
		</method>
		<method name="tween_property">
			<return type="PropertyTweener">
			</return>
			<argument index="0" name="object" type="Object">
			</argument>
			<argument index="1" name="property" type="NodePath">
			</argument>
			<argument index="2" name="final_val" type="Variant">
			</argument>
			<argument index="3" name="duration" type="float">
			</argument>
			<description>
				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:
				[codeblock]
				var tween = create_tween()
				tween.tween_property($Sprite, "position", Vector2(100, 200)
				tween.tween_property($Sprite, "position", Vector2(200, 300)
				[/codeblock]
				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.
				[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.
				Example: moving object twice from the same position, with different transition types.
				[codeblock]
				var tween = create_tween()
				tween.tween_property($Sprite, "position", Vector2.RIGHT * 300).as_relative().set_trans(Tween.TRANS_SINE)
				tween.tween_property($Sprite, "position", Vector2.RIGHT * 300).as_relative().from_current().set_trans(Tween.TRANS_EXPO)
				[/codeblock]
			</description>
		</method>
	</methods>
	<signals>
		<signal name="finished">
			<description>
				Emitted when the [Tween] has finished all tweening. Never emitted when the [Tween] is set to infinite looping (see [method set_loops]).
				[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].
			</description>
		</signal>
		<signal name="loop_finished">
			<argument index="0" name="loop_count" type="int">
			</argument>
			<description>
				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.
			</description>
		</signal>
		<signal name="step_finished">
			<argument index="0" name="idx" type="int">
			</argument>
			<description>
				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.
			</description>
		</signal>
	</signals>
	<constants>
		<constant name="TWEEN_PROCESS_PHYSICS" value="0" enum="TweenProcessMode">
			The [Tween] updates during physics frame.
		</constant>
		<constant name="TWEEN_PROCESS_IDLE" value="1" enum="TweenProcessMode">
			The [Tween] updates during idle
		</constant>
		<constant name="TWEEN_PAUSE_BOUND" value="0" enum="TweenPauseMode">
		</constant>
		<constant name="TWEEN_PAUSE_STOP" value="1" enum="TweenPauseMode">
		</constant>
		<constant name="TWEEN_PAUSE_PROCESS" value="2" enum="TweenPauseMode">
		</constant>
		<constant name="TRANS_LINEAR" value="0" enum="TransitionType">
		</constant>
		<constant name="TRANS_SINE" value="1" enum="TransitionType">
		</constant>
		<constant name="TRANS_QUINT" value="2" enum="TransitionType">
		</constant>
		<constant name="TRANS_QUART" value="3" enum="TransitionType">
		</constant>
		<constant name="TRANS_QUAD" value="4" enum="TransitionType">
		</constant>
		<constant name="TRANS_EXPO" value="5" enum="TransitionType">
		</constant>
		<constant name="TRANS_ELASTIC" value="6" enum="TransitionType">
		</constant>
		<constant name="TRANS_CUBIC" value="7" enum="TransitionType">
		</constant>
		<constant name="TRANS_CIRC" value="8" enum="TransitionType">
		</constant>
		<constant name="TRANS_BOUNCE" value="9" enum="TransitionType">
		</constant>
		<constant name="TRANS_BACK" value="10" enum="TransitionType">
		</constant>
		<constant name="EASE_IN" value="0" enum="EaseType">
		</constant>
		<constant name="EASE_OUT" value="1" enum="EaseType">
		</constant>
		<constant name="EASE_IN_OUT" value="2" enum="EaseType">
		</constant>
		<constant name="EASE_OUT_IN" value="3" enum="EaseType">
		</constant>
	</constants>
</class>