Skip to content
0.2.0-dev Download ZIP
698 lines ·35.8 KB Raw
1<?xml version="1.0" encoding="UTF-8" ?>
2<class name="Tween" inherits="RefCounted" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
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. Animating something with a [Tween] is called tweening.
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 and can't be used for tweening values.
10 A tween animation is created by adding [Tweener]s to the [Tween] object, using [method tween_property], [method tween_interval], [method tween_callback], [method tween_method], [method tween_subtween], or [method tween_await]:
11 [codeblocks]
12 [gdscript]
13 var tween = get_tree().create_tween()
14 tween.tween_property($Sprite, "modulate", Color.RED, 1.0)
15 tween.tween_property($Sprite, "scale", Vector2(), 1.0)
16 tween.tween_callback($Sprite.queue_free)
17 [/gdscript]
18 [csharp]
19 Tween tween = GetTree().CreateTween();
20 tween.TweenProperty(GetNode("Sprite"), "modulate", Colors.Red, 1.0f);
21 tween.TweenProperty(GetNode("Sprite"), "scale", Vector2.Zero, 1.0f);
22 tween.TweenCallback(Callable.From(GetNode("Sprite").QueueFree));
23 [/csharp]
24 [/codeblocks]
25 This sequence will make the [code]$Sprite[/code] node turn red, then shrink, before finally calling [method Node.queue_free] to free the sprite. [Tweener]s are executed one after another by default. This behavior can be changed using [method parallel] and [method set_parallel].
26 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 a different transition type in the above example, you can use [method set_trans]:
27 [codeblocks]
28 [gdscript]
29 var tween = get_tree().create_tween()
30 tween.tween_property($Sprite, "modulate", Color.RED, 1.0).set_trans(Tween.TRANS_SINE)
31 tween.tween_property($Sprite, "scale", Vector2(), 1.0).set_trans(Tween.TRANS_BOUNCE)
32 tween.tween_callback($Sprite.queue_free)
33 [/gdscript]
34 [csharp]
35 Tween tween = GetTree().CreateTween();
36 tween.TweenProperty(GetNode("Sprite"), "modulate", Colors.Red, 1.0f).SetTrans(Tween.TransitionType.Sine);
37 tween.TweenProperty(GetNode("Sprite"), "scale", Vector2.Zero, 1.0f).SetTrans(Tween.TransitionType.Bounce);
38 tween.TweenCallback(Callable.From(GetNode("Sprite").QueueFree));
39 [/csharp]
40 [/codeblocks]
41 Most of the [Tween] methods can be chained this way too. In the following example the [Tween] is bound to the running script's node and a default transition is set for its [Tweener]s:
42 [codeblocks]
43 [gdscript]
44 var tween = get_tree().create_tween().bind_node(self).set_trans(Tween.TRANS_ELASTIC)
45 tween.tween_property($Sprite, "modulate", Color.RED, 1.0)
46 tween.tween_property($Sprite, "scale", Vector2(), 1.0)
47 tween.tween_callback($Sprite.queue_free)
48 [/gdscript]
49 [csharp]
50 var tween = GetTree().CreateTween().BindNode(this).SetTrans(Tween.TransitionType.Elastic);
51 tween.TweenProperty(GetNode("Sprite"), "modulate", Colors.Red, 1.0f);
52 tween.TweenProperty(GetNode("Sprite"), "scale", Vector2.Zero, 1.0f);
53 tween.TweenCallback(Callable.From(GetNode("Sprite").QueueFree));
54 [/csharp]
55 [/codeblocks]
56 Another interesting use for [Tween]s is animating arbitrary sets of objects:
57 [codeblocks]
58 [gdscript]
59 var tween = create_tween()
60 for sprite in get_children():
61 tween.tween_property(sprite, "position", Vector2(0, 0), 1.0)
62 [/gdscript]
63 [csharp]
64 Tween tween = CreateTween();
65 foreach (Node sprite in GetChildren())
66 tween.TweenProperty(sprite, "position", Vector2.Zero, 1.0f);
67 [/csharp]
68 [/codeblocks]
69 In the example above, all children of a node are moved one after another to position [code](0, 0)[/code].
70 You should avoid using more than one [Tween] per object's property. If two or more tweens animate one property at the same time, the last one created will take priority and assign the final value. If you want to interrupt and restart an animation, consider assigning the [Tween] to a variable:
71 [codeblocks]
72 [gdscript]
73 var tween
74 func animate():
75 if tween:
76 tween.kill() # Abort the previous animation.
77 tween = create_tween()
78 [/gdscript]
79 [csharp]
80 private Tween _tween;
81
82 public void Animate()
83 {
84 if (_tween != null)
85 _tween.Kill(); // Abort the previous animation
86 _tween = CreateTween();
87 }
88 [/csharp]
89 [/codeblocks]
90 Some [Tweener]s use transitions and eases. The first accepts a [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.
91 [url=https://raw.githubusercontent.com/godotengine/godot-docs/master/img/tween_cheatsheet.webp]Tween easing and transition types cheatsheet[/url]
92 [b]Note:[/b] Tweens are not designed to be reused and trying to do so results in an undefined behavior. Create a new Tween for each animation and every time you replay an animation from start. Keep in mind that Tweens start immediately, so only create a Tween when you want to start animating.
93 [b]Note:[/b] The tween is processed after all of the nodes in the current frame, i.e. node's [method Node._process] method would be called before the tween (or [method Node._physics_process] depending on the value passed to [method set_process_mode]).
94 </description>
95 <tutorials>
96 </tutorials>
97 <methods>
98 <method name="bind_node">
99 <return type="Tween" />
100 <param index="0" name="node" type="Node" />
101 <description>
102 Binds this [Tween] with the given [param node]. [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.
103 For a shorter way to create and bind a [Tween], you can use [method Node.create_tween].
104 </description>
105 </method>
106 <method name="chain">
107 <return type="Tween" />
108 <description>
109 Used to chain two [Tweener]s after [method set_parallel] is called with [code]true[/code].
110 [codeblocks]
111 [gdscript]
112 var tween = create_tween().set_parallel(true)
113 tween.tween_property(...)
114 tween.tween_property(...) # Will run parallelly with above.
115 tween.chain().tween_property(...) # Will run after two above are finished.
116 [/gdscript]
117 [csharp]
118 Tween tween = CreateTween().SetParallel(true);
119 tween.TweenProperty(...);
120 tween.TweenProperty(...); // Will run parallelly with above.
121 tween.Chain().TweenProperty(...); // Will run after two above are finished.
122 [/csharp]
123 [/codeblocks]
124 </description>
125 </method>
126 <method name="custom_step">
127 <return type="bool" />
128 <param index="0" name="delta" type="float" />
129 <description>
130 Processes the [Tween] by the given [param delta] value, in seconds. This is mostly useful for manual control when the [Tween] is paused. It can also be used to end the [Tween] animation immediately, by setting [param delta] longer than the whole duration of the [Tween] animation.
131 Returns [code]true[/code] if the [Tween] still has [Tweener]s that haven't finished.
132 </description>
133 </method>
134 <method name="get_loops_left" qualifiers="const">
135 <return type="int" />
136 <description>
137 Returns the number of remaining loops for this [Tween] (see [method set_loops]). A return value of [code]-1[/code] indicates an infinitely looping [Tween], and a return value of [code]0[/code] indicates that the [Tween] has already finished.
138 </description>
139 </method>
140 <method name="get_total_elapsed_time" qualifiers="const">
141 <return type="float" />
142 <description>
143 Returns the total time in seconds the [Tween] has been animating (i.e. the time since it started, not counting pauses etc.). The time is affected by [method set_speed_scale], and [method stop] will reset it to [code]0[/code].
144 [b]Note:[/b] As it results from accumulating frame deltas, the time returned after the [Tween] has finished animating will be slightly greater than the actual [Tween] duration.
145 </description>
146 </method>
147 <method name="has_tweeners" qualifiers="const">
148 <return type="bool" />
149 <description>
150 Returns [code]true[/code] if any [Tweener] has been added to the [Tween] and the [Tween] is valid. Useful when tweeners are added dynamically and the tween can end up empty. Killing an empty tween before it starts will prevent errors.
151 </description>
152 </method>
153 <method name="interpolate_value" qualifiers="static">
154 <return type="Variant" />
155 <param index="0" name="initial_value" type="Variant" />
156 <param index="1" name="delta_value" type="Variant" />
157 <param index="2" name="elapsed_time" type="float" />
158 <param index="3" name="duration" type="float" />
159 <param index="4" name="trans_type" type="int" enum="Tween.TransitionType" />
160 <param index="5" name="ease_type" type="int" enum="Tween.EaseType" />
161 <description>
162 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.
163 [param initial_value] is the starting value of the interpolation.
164 [param delta_value] is the change of the value in the interpolation, i.e. it's equal to [code]final_value - initial_value[/code].
165 [param elapsed_time] 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 [param duration], the interpolated value will be halfway between initial and final values. This value can also be greater than [param duration] or lower than 0, which will extrapolate the value.
166 [param duration] is the total time of the interpolation.
167 [b]Note:[/b] If [param duration] is equal to [code]0[/code], the method will always return the final value, regardless of [param elapsed_time] provided.
168 </description>
169 </method>
170 <method name="is_running">
171 <return type="bool" />
172 <description>
173 Returns whether the [Tween] is currently running, i.e. it wasn't paused and it's not finished.
174 </description>
175 </method>
176 <method name="is_valid">
177 <return type="bool" />
178 <description>
179 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]). A [Tween] might become invalid when it has finished tweening, is killed, or when created with [code]Tween.new()[/code]. Invalid [Tween]s can't have [Tweener]s appended.
180 </description>
181 </method>
182 <method name="kill">
183 <return type="void" />
184 <description>
185 Aborts all tweening operations and invalidates the [Tween].
186 </description>
187 </method>
188 <method name="parallel">
189 <return type="Tween" />
190 <description>
191 Makes the next [Tweener] run parallelly to the previous one.
192 [codeblocks]
193 [gdscript]
194 var tween = create_tween()
195 tween.tween_property(...)
196 tween.parallel().tween_property(...)
197 tween.parallel().tween_property(...)
198 [/gdscript]
199 [csharp]
200 Tween tween = CreateTween();
201 tween.TweenProperty(...);
202 tween.Parallel().TweenProperty(...);
203 tween.Parallel().TweenProperty(...);
204 [/csharp]
205 [/codeblocks]
206 All [Tweener]s in the example will run at the same time.
207 You can make the [Tween] parallel by default by using [method set_parallel].
208 </description>
209 </method>
210 <method name="pause">
211 <return type="void" />
212 <description>
213 Pauses the tweening. The animation can be resumed by using [method play].
214 [b]Note:[/b] If a Tween is paused and not bound to any node, it will exist indefinitely until manually started or invalidated. If you lose a reference to such Tween, you can retrieve it using [method SceneTree.get_processed_tweens].
215 </description>
216 </method>
217 <method name="play">
218 <return type="void" />
219 <description>
220 Resumes a paused or stopped [Tween].
221 </description>
222 </method>
223 <method name="set_ease">
224 <return type="Tween" />
225 <param index="0" name="ease" type="int" enum="Tween.EaseType" />
226 <description>
227 Sets the default ease type for [PropertyTweener]s and [MethodTweener]s appended after this method.
228 Before this method is called, the default ease type is [constant EASE_IN_OUT].
229 [codeblocks]
230 [gdscript]
231 var tween = create_tween()
232 tween.tween_property(self, "position", Vector2(300, 0), 0.5) # Uses EASE_IN_OUT.
233 tween.set_ease(Tween.EASE_IN)
234 tween.tween_property(self, "rotation_degrees", 45.0, 0.5) # Uses EASE_IN.
235 [/gdscript]
236 [csharp]
237 Tween tween = CreateTween();
238 tween.TweenProperty(this, "position", new Vector2(300, 0), 0.5); // Uses EaseType.InOut.
239 tween.SetEase(Tween.EaseType.In);
240 tween.TweenProperty(this, "rotation_degrees", 45.0, 0.5); // Uses EaseType.In.
241 [/csharp]
242 [/codeblocks]
243 </description>
244 </method>
245 <method name="set_ignore_time_scale">
246 <return type="Tween" />
247 <param index="0" name="ignore" type="bool" default="true" />
248 <description>
249 If [param ignore] is [code]true[/code], the tween will ignore [member Engine.time_scale] and update with the real, elapsed time. This affects all [Tweener]s and their delays. Default value is [code]false[/code].
250 </description>
251 </method>
252 <method name="set_loops">
253 <return type="Tween" />
254 <param index="0" name="loops" type="int" default="0" />
255 <description>
256 Sets the number of times the tweening sequence will be repeated, i.e. [code]set_loops(2)[/code] will run the animation twice.
257 Calling this method without arguments will make the [Tween] run infinitely, until either it is killed with [method kill], the [Tween]'s bound node is freed, or all the animated objects have been freed (which makes further animation impossible).
258 [b]Warning:[/b] Make sure to always add some duration/delay when using infinite loops. To prevent the game freezing, 0-duration looped animations (e.g. a single [CallbackTweener] with no delay) are stopped after a small number of loops, which may produce unexpected results. If a [Tween]'s lifetime depends on some node, always use [method bind_node].
259 </description>
260 </method>
261 <method name="set_parallel">
262 <return type="Tween" />
263 <param index="0" name="parallel" type="bool" default="true" />
264 <description>
265 If [param parallel] is [code]true[/code], the [Tweener]s appended after this method will by default run simultaneously, as opposed to sequentially.
266 [b]Note:[/b] Just like with [method parallel], the tweener added right before this method will also be part of the parallel step.
267 [codeblocks]
268 [gdscript]
269 tween.tween_property(self, "position", Vector2(300, 0), 0.5)
270 tween.set_parallel()
271 tween.tween_property(self, "modulate", Color.GREEN, 0.5) # Runs together with the position tweener.
272 [/gdscript]
273 [csharp]
274 tween.TweenProperty(this, "position", new Vector2(300, 0), 0.5f);
275 tween.SetParallel();
276 tween.TweenProperty(this, "modulate", Colors.Green, 0.5f); // Runs together with the position tweener.
277 [/csharp]
278 [/codeblocks]
279 </description>
280 </method>
281 <method name="set_pause_mode">
282 <return type="Tween" />
283 <param index="0" name="mode" type="int" enum="Tween.TweenPauseMode" />
284 <description>
285 Determines the behavior of the [Tween] when the [SceneTree] is paused.
286 Default value is [constant TWEEN_PAUSE_BOUND].
287 </description>
288 </method>
289 <method name="set_process_mode">
290 <return type="Tween" />
291 <param index="0" name="mode" type="int" enum="Tween.TweenProcessMode" />
292 <description>
293 Determines whether the [Tween] should run after process frames (see [method Node._process]) or physics frames (see [method Node._physics_process]).
294 Default value is [constant TWEEN_PROCESS_IDLE].
295 </description>
296 </method>
297 <method name="set_speed_scale">
298 <return type="Tween" />
299 <param index="0" name="speed" type="float" />
300 <description>
301 Scales the speed of tweening. This affects all [Tweener]s and their delays.
302 </description>
303 </method>
304 <method name="set_trans">
305 <return type="Tween" />
306 <param index="0" name="trans" type="int" enum="Tween.TransitionType" />
307 <description>
308 Sets the default transition type for [PropertyTweener]s and [MethodTweener]s appended after this method.
309 Before this method is called, the default transition type is [constant TRANS_LINEAR].
310 [codeblocks]
311 [gdscript]
312 var tween = create_tween()
313 tween.tween_property(self, "position", Vector2(300, 0), 0.5) # Uses TRANS_LINEAR.
314 tween.set_trans(Tween.TRANS_SINE)
315 tween.tween_property(self, "rotation_degrees", 45.0, 0.5) # Uses TRANS_SINE.
316 [/gdscript]
317 [csharp]
318 Tween tween = CreateTween();
319 tween.TweenProperty(this, "position", new Vector2(300, 0), 0.5f); // Uses TransitionType.Linear.
320 tween.SetTrans(Tween.TransitionType.Sine);
321 tween.TweenProperty(this, "rotation_degrees", 45.0f, 0.5f); // Uses TransitionType.Sine.
322 [/csharp]
323 [/codeblocks]
324 </description>
325 </method>
326 <method name="stop">
327 <return type="void" />
328 <description>
329 Stops the tweening and resets the [Tween] to its initial state. This will not remove any appended [Tweener]s.
330 [b]Note:[/b] This does [i]not[/i] reset targets of [PropertyTweener]s to their values when the [Tween] first started.
331 [codeblocks]
332 [gdscript]
333 var tween = create_tween()
334
335 # Will move from 0 to 500 over 1 second.
336 position.x = 0.0
337 tween.tween_property(self, "position:x", 500, 1.0)
338
339 # Will be at (about) 250 when the timer finishes.
340 await get_tree().create_timer(0.5).timeout
341
342 # Will now move from (about) 250 to 500 over 1 second,
343 # thus at half the speed as before.
344 tween.stop()
345 tween.play()
346 [/gdscript]
347 [csharp]
348 Tween tween = CreateTween();
349
350 // Will move from 0 to 500 over 1 second.
351 Position = Position with { X = 0.0f };
352 tween.TweenProperty(this, "position:x", 500.0f, 1.0f);
353
354 // Will be at (about) 250 when the timer finishes.
355 await ToSignal(GetTree().CreateTimer(0.5f), Timer.SignalName.Timeout);
356
357 // Will now move from (about) 250 to 500 over 1 second,
358 // thus at half the speed as before.
359 tween.Stop();
360 tween.Play();
361 [/csharp]
362 [/codeblocks]
363 [b]Note:[/b] If a Tween is stopped and not bound to any node, it will exist indefinitely until manually started or invalidated. If you lose a reference to such Tween, you can retrieve it using [method SceneTree.get_processed_tweens].
364 </description>
365 </method>
366 <method name="tween_await">
367 <return type="AwaitTweener" />
368 <param index="0" name="signal" type="Signal" />
369 <description>
370 Creates and appends an [AwaitTweener]. This method can be used to await a signal to be emitted and create asynchronous animations or cutscenes.
371 The animation will not progress to the next step until the awaited signal is emitted or the connection becomes invalid (e.g. as a result of freeing the target object). If you know that the emission may not happen, use [method AwaitTweener.set_timeout].
372 [b]Note:[/b] The awaited signal should be emitted during the step when [AwaitTweener] is active.
373 [b]Example:[/b] An object launches itself and explodes upon collision or after 4 seconds.
374 [codeblocks]
375 [gdscript]
376 var tween = create_tween()
377 tween.tween_callback(launch)
378 tween.tween_await(collided).set_timeout(4.0)
379 tween.tween_callback(explode)
380 [/gdscript]
381 [csharp]
382 Tween tween = CreateTween();
383 tween.TweenCallback(Callable.From(Launch));
384 tween.TweenAwait(new Signal(this, SignalName.Collided)).SetTimeout(4.0f);
385 tween.TweenCallback(Callable.From(Explode));
386 [/csharp]
387 [/codeblocks]
388 [b]Example:[/b] A character walks to a specific point, says some lines and walks back when the player closes the message box.
389 [codeblocks]
390 [gdscript]
391 var tween = create_tween()
392 tween.tween_callback(walk_to.bind(600.0))
393 tween.tween_await(destination_reached)
394 tween.tween_callback(say_dialogue.bind("Good day, sir!"))
395 tween.tween_await(dialogue_closed)
396 tween.tween_callback(walk_to.bind(0.0))
397 [/gdscript]
398 [csharp]
399 Tween tween = CreateTween();
400 tween.TweenCallback(Callable.From(() =&gt; WalkTo(600.0f)));
401 tween.TweenAwait(new Signal(this, SignalName.DestinationReached));
402 tween.TweenCallback(Callable.From(() =&gt; SayDialogue("Good day, sir!")));
403 tween.TweenAwait(new Signal(this, SignalName.DialogueClosed));
404 tween.TweenCallback(Callable.From(() =&gt; WalkTo(0.0f)));
405 [/csharp]
406 [/codeblocks]
407 [b]Note:[/b] If you are awaiting a signal from a callback called in the same [Tween], make sure the signal is emitted [i]after[/i] the await starts. If it can't be reasonably guaranteed, you can await and emit in the same step:
408 [codeblocks]
409 [gdscript]
410 var tween = create_tween()
411 tween.tween_await(signal)
412 tween.parallel().tween_callback(method_that_emits_signal)
413 [/gdscript]
414 [csharp]
415 Tween tween = CreateTween();
416 tween.TweenAwait(new Signal(this, SignalName.Signal));
417 tween.Parallel().TweenCallback(Callable.From(MethodThatEmitsSignal));
418 [/csharp]
419 [/codeblocks]
420 </description>
421 </method>
422 <method name="tween_callback">
423 <return type="CallbackTweener" />
424 <param index="0" name="callback" type="Callable" />
425 <description>
426 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.
427 [b]Example:[/b] Object that keeps shooting every 1 second:
428 [codeblocks]
429 [gdscript]
430 var tween = get_tree().create_tween().set_loops()
431 tween.tween_callback(shoot).set_delay(1.0)
432 [/gdscript]
433 [csharp]
434 Tween tween = GetTree().CreateTween().SetLoops();
435 tween.TweenCallback(Callable.From(Shoot)).SetDelay(1.0f);
436 [/csharp]
437 [/codeblocks]
438 [b]Example:[/b] Turning a sprite red and then blue, with 2 second delay:
439 [codeblocks]
440 [gdscript]
441 var tween = get_tree().create_tween()
442 tween.tween_callback($Sprite.set_modulate.bind(Color.RED)).set_delay(2)
443 tween.tween_callback($Sprite.set_modulate.bind(Color.BLUE)).set_delay(2)
444 [/gdscript]
445 [csharp]
446 Tween tween = GetTree().CreateTween();
447 Sprite2D sprite = GetNode&lt;Sprite2D&gt;("Sprite");
448 tween.TweenCallback(Callable.From(() =&gt; sprite.Modulate = Colors.Red)).SetDelay(2.0f);
449 tween.TweenCallback(Callable.From(() =&gt; sprite.Modulate = Colors.Blue)).SetDelay(2.0f);
450 [/csharp]
451 [/codeblocks]
452 </description>
453 </method>
454 <method name="tween_interval">
455 <return type="IntervalTweener" />
456 <param index="0" name="time" type="float" />
457 <description>
458 Creates and appends an [IntervalTweener]. This method can be used to create delays in the tween animation, as an alternative to using the delay in other [Tweener]s, or when there's no animation (in which case the [Tween] acts as a timer). [param time] is the length of the interval, in seconds.
459 [b]Example:[/b] Creating an interval in code execution:
460 [codeblocks]
461 [gdscript]
462 # ... some code
463 await create_tween().tween_interval(2).finished
464 # ... more code
465 [/gdscript]
466 [csharp]
467 // ... some code
468 await ToSignal(CreateTween().TweenInterval(2.0f), Tween.SignalName.Finished);
469 // ... more code
470 [/csharp]
471 [/codeblocks]
472 [b]Example:[/b] Creating an object that moves back and forth and jumps every few seconds:
473 [codeblocks]
474 [gdscript]
475 var tween = create_tween().set_loops()
476 tween.tween_property($Sprite, "position:x", 200.0, 1.0).as_relative()
477 tween.tween_callback(jump)
478 tween.tween_interval(2)
479 tween.tween_property($Sprite, "position:x", -200.0, 1.0).as_relative()
480 tween.tween_callback(jump)
481 tween.tween_interval(2)
482 [/gdscript]
483 [csharp]
484 Tween tween = CreateTween().SetLoops();
485 tween.TweenProperty(GetNode("Sprite"), "position:x", 200.0f, 1.0f).AsRelative();
486 tween.TweenCallback(Callable.From(Jump));
487 tween.TweenInterval(2.0f);
488 tween.TweenProperty(GetNode("Sprite"), "position:x", -200.0f, 1.0f).AsRelative();
489 tween.TweenCallback(Callable.From(Jump));
490 tween.TweenInterval(2.0f);
491 [/csharp]
492 [/codeblocks]
493 </description>
494 </method>
495 <method name="tween_method">
496 <return type="MethodTweener" />
497 <param index="0" name="method" type="Callable" />
498 <param index="1" name="from" type="Variant" />
499 <param index="2" name="to" type="Variant" />
500 <param index="3" name="duration" type="float" />
501 <description>
502 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 [param from] and [param to] over the time specified by [param duration], 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.
503 [b]Example:[/b] Making a 3D object look from one point to another point:
504 [codeblocks]
505 [gdscript]
506 var tween = create_tween()
507 tween.tween_method(look_at.bind(Vector3.UP), Vector3(-1, 0, -1), Vector3(1, 0, -1), 1.0) # The look_at() method takes up vector as second argument.
508 [/gdscript]
509 [csharp]
510 Tween tween = CreateTween();
511 tween.TweenMethod(Callable.From((Vector3 target) =&gt; LookAt(target, Vector3.Up)), new Vector3(-1.0f, 0.0f, -1.0f), new Vector3(1.0f, 0.0f, -1.0f), 1.0f); // Use lambdas to bind additional arguments for the call.
512 [/csharp]
513 [/codeblocks]
514 [b]Example:[/b] Setting the text of a [Label], using an intermediate method and after a delay:
515 [codeblocks]
516 [gdscript]
517 func _ready():
518 var tween = create_tween()
519 tween.tween_method(set_label_text, 0, 10, 1.0).set_delay(1.0)
520
521 func set_label_text(value: int):
522 $Label.text = "Counting " + str(value)
523 [/gdscript]
524 [csharp]
525 public override void _Ready()
526 {
527 base._Ready();
528
529 Tween tween = CreateTween();
530 tween.TweenMethod(Callable.From&lt;int&gt;(SetLabelText), 0.0f, 10.0f, 1.0f).SetDelay(1.0f);
531 }
532
533 private void SetLabelText(int value)
534 {
535 GetNode&lt;Label&gt;("Label").Text = $"Counting {value}";
536 }
537 [/csharp]
538 [/codeblocks]
539 </description>
540 </method>
541 <method name="tween_property">
542 <return type="PropertyTweener" />
543 <param index="0" name="object" type="Object" />
544 <param index="1" name="property" type="NodePath" />
545 <param index="2" name="final_val" type="Variant" />
546 <param index="3" name="duration" type="float" />
547 <description>
548 Creates and appends a [PropertyTweener]. This method tweens a [param property] of an [param object] between an initial value and [param final_val] in a span of time equal to [param duration], in seconds. The initial value by default is the property's value at the time the tweening of the [PropertyTweener] starts.
549 [codeblocks]
550 [gdscript]
551 var tween = create_tween()
552 tween.tween_property($Sprite, "position", Vector2(100, 200), 1.0)
553 tween.tween_property($Sprite, "position", Vector2(200, 300), 1.0)
554 [/gdscript]
555 [csharp]
556 Tween tween = CreateTween();
557 tween.TweenProperty(GetNode("Sprite"), "position", new Vector2(100.0f, 200.0f), 1.0f);
558 tween.TweenProperty(GetNode("Sprite"), "position", new Vector2(200.0f, 300.0f), 1.0f);
559 [/csharp]
560 [/codeblocks]
561 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.
562 [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.
563 [b]Example:[/b] Moving an object twice from the same position, with different transition types:
564 [codeblocks]
565 [gdscript]
566 var tween = create_tween()
567 tween.tween_property($Sprite, "position", Vector2.RIGHT * 300, 1.0).as_relative().set_trans(Tween.TRANS_SINE)
568 tween.tween_property($Sprite, "position", Vector2.RIGHT * 300, 1.0).as_relative().from_current().set_trans(Tween.TRANS_EXPO)
569 [/gdscript]
570 [csharp]
571 Tween tween = CreateTween();
572 tween.TweenProperty(GetNode("Sprite"), "position", Vector2.Right * 300.0f, 1.0f).AsRelative().SetTrans(Tween.TransitionType.Sine);
573 tween.TweenProperty(GetNode("Sprite"), "position", Vector2.Right * 300.0f, 1.0f).AsRelative().FromCurrent().SetTrans(Tween.TransitionType.Expo);
574 [/csharp]
575 [/codeblocks]
576 </description>
577 </method>
578 <method name="tween_subtween">
579 <return type="SubtweenTweener" />
580 <param index="0" name="subtween" type="Tween" />
581 <description>
582 Creates and appends a [SubtweenTweener]. This method can be used to nest [param subtween] within this [Tween], allowing for the creation of more complex and composable sequences.
583 [codeblocks]
584 [gdscript]
585 # Subtween will rotate the object.
586 var subtween = create_tween()
587 subtween.tween_property(self, "rotation_degrees", 45.0, 1.0)
588 subtween.tween_property(self, "rotation_degrees", 0.0, 1.0)
589
590 # Parent tween will execute the subtween as one of its steps.
591 var tween = create_tween()
592 tween.tween_property(self, "position:x", 500, 3.0)
593 tween.tween_subtween(subtween)
594 tween.tween_property(self, "position:x", 300, 2.0)
595 [/gdscript]
596 [csharp]
597 // Subtween will rotate the object.
598 Tween subtween = CreateTween();
599 subtween.TweenProperty(this, "rotation_degrees", 45.0f, 1.0f);
600 subtween.TweenProperty(this, "rotation_degrees", 0.0f, 1.0f);
601
602 // Parent tween will execute the subtween as one of its steps.
603 Tween tween = CreateTween();
604 tween.TweenProperty(this, "position:x", 500.0f, 3.0f);
605 tween.TweenSubtween(subtween);
606 tween.TweenProperty(this, "position:x", 300.0f, 2.0f);
607 [/csharp]
608 [/codeblocks]
609 [b]Note:[/b] The methods [method pause], [method stop], and [method set_loops] can cause the parent [Tween] to get stuck on the subtween step; see the documentation for those methods for more information.
610 [b]Note:[/b] The pause and process modes set by [method set_pause_mode] and [method set_process_mode] on [param subtween] will be overridden by the parent [Tween]'s settings.
611 </description>
612 </method>
613 </methods>
614 <signals>
615 <signal name="finished">
616 <description>
617 Emitted when the [Tween] has finished all tweening. Never emitted when the [Tween] is set to infinite looping (see [method set_loops]).
618 </description>
619 </signal>
620 <signal name="loop_finished">
621 <param index="0" name="loop_count" type="int" />
622 <description>
623 Emitted when a full loop is complete (see [method set_loops]), providing the loop index. This signal is not emitted after the final loop, use [signal finished] instead for this case.
624 </description>
625 </signal>
626 <signal name="step_finished">
627 <param index="0" name="idx" type="int" />
628 <description>
629 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 in parallel.
630 </description>
631 </signal>
632 </signals>
633 <constants>
634 <constant name="TWEEN_PROCESS_PHYSICS" value="0" enum="TweenProcessMode">
635 The [Tween] updates after each physics frame (see [method Node._physics_process]).
636 </constant>
637 <constant name="TWEEN_PROCESS_IDLE" value="1" enum="TweenProcessMode">
638 The [Tween] updates after each process frame (see [method Node._process]).
639 </constant>
640 <constant name="TWEEN_PAUSE_BOUND" value="0" enum="TweenPauseMode">
641 If the [Tween] has a bound node, it will process when that node can process (see [member Node.process_mode]). Otherwise it's the same as [constant TWEEN_PAUSE_STOP].
642 </constant>
643 <constant name="TWEEN_PAUSE_STOP" value="1" enum="TweenPauseMode">
644 If [SceneTree] is paused, the [Tween] will also pause.
645 </constant>
646 <constant name="TWEEN_PAUSE_PROCESS" value="2" enum="TweenPauseMode">
647 The [Tween] will process regardless of whether [SceneTree] is paused.
648 </constant>
649 <constant name="TRANS_LINEAR" value="0" enum="TransitionType">
650 The animation is interpolated linearly.
651 </constant>
652 <constant name="TRANS_SINE" value="1" enum="TransitionType">
653 The animation is interpolated using a sine function.
654 </constant>
655 <constant name="TRANS_QUINT" value="2" enum="TransitionType">
656 The animation is interpolated with a quintic (to the power of 5) function.
657 </constant>
658 <constant name="TRANS_QUART" value="3" enum="TransitionType">
659 The animation is interpolated with a quartic (to the power of 4) function.
660 </constant>
661 <constant name="TRANS_QUAD" value="4" enum="TransitionType">
662 The animation is interpolated with a quadratic (to the power of 2) function.
663 </constant>
664 <constant name="TRANS_EXPO" value="5" enum="TransitionType">
665 The animation is interpolated with an exponential (to the power of x) function.
666 </constant>
667 <constant name="TRANS_ELASTIC" value="6" enum="TransitionType">
668 The animation is interpolated with elasticity, wiggling around the edges.
669 </constant>
670 <constant name="TRANS_CUBIC" value="7" enum="TransitionType">
671 The animation is interpolated with a cubic (to the power of 3) function.
672 </constant>
673 <constant name="TRANS_CIRC" value="8" enum="TransitionType">
674 The animation is interpolated with a function using square roots.
675 </constant>
676 <constant name="TRANS_BOUNCE" value="9" enum="TransitionType">
677 The animation is interpolated by bouncing at the end.
678 </constant>
679 <constant name="TRANS_BACK" value="10" enum="TransitionType">
680 The animation is interpolated backing out at ends.
681 </constant>
682 <constant name="TRANS_SPRING" value="11" enum="TransitionType">
683 The animation is interpolated like a spring towards the end.
684 </constant>
685 <constant name="EASE_IN" value="0" enum="EaseType">
686 The interpolation starts slowly and speeds up towards the end.
687 </constant>
688 <constant name="EASE_OUT" value="1" enum="EaseType">
689 The interpolation starts quickly and slows down towards the end.
690 </constant>
691 <constant name="EASE_IN_OUT" value="2" enum="EaseType">
692 A combination of [constant EASE_IN] and [constant EASE_OUT]. The interpolation is slowest at both ends.
693 </constant>
694 <constant name="EASE_OUT_IN" value="3" enum="EaseType">
695 A combination of [constant EASE_IN] and [constant EASE_OUT]. The interpolation is fastest at both ends.
696 </constant>
697 </constants>
698</class>