Lightweight object used for general-purpose animation via script, using Tweeners.
Tweens are mostly useful for animations requiring a numerical property to be interpolated over a range of values. The name tween comes from in-betweening, an animation technique where you specify keyframes 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.
Tween animation is composed of a sequence of Tweeners, which by default are executed one after another. You can create a sequence by appending Tweeners to the
Tween. Animating something with a Tweener is called tweening. Example tweening sequence looks like this:
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)
This sequence will make the
$Sprite node turn red, then shrink and finally the Node.queue_free is called to remove the sprite. See methods tween_property, tween_interval, tween_callback and tween_method for more usage information.
When a Tweener is created with one of the
tween_* 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:
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)
Most of the
Tween methods can be chained this way too. In this example the
Tween is bound and have set a default transition:
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)
Another interesting use for
Tweens is animating arbitrary set of objects:
var tween = create_tween() for sprite in get_children(): tween.tween_property(sprite, "position", Vector2(), 1)
In the example above, all children of a node are moved one after another to position (0, 0).
Some Tweeners use transitions and eases. The first accepts an TransitionType constant, and refers to the way the timing of the animation is handled (see easings.net for some examples). The second accepts an EaseType constant, and controls where the
trans_type 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 TransitionType constants with EASE_IN_OUT, and use the one that looks best.
Tweens will automatically start by default. To prevent a
Tween from autostarting, you can call stop immediately after it was created.
chain ( )
get_total_elapsed_time ( ) const
is_running ( )
is_valid ( )
kill ( )
parallel ( )
pause ( )
play ( )
stop ( )
finished ( )
Emitted when the
Tween has finished all tweening. Never emitted when the
Tween is set to infinite looping (see set_loops).
Tween is removed (invalidated) after this signal is emitted, but it doesn't happen immediately, but on the next processing frame. Calling stop inside the signal callback will preserve the
loop_finished ( int loop_count )
step_finished ( int idx )
TWEEN_PROCESS_PHYSICS = 0 --- The
Tweenupdates during the physics frame.
TWEEN_PROCESS_IDLE = 1 --- The
Tweenupdates during the idle frame.
TWEEN_PAUSE_STOP = 1 --- If SceneTree is paused, the
Tweenwill also pause.
TWEEN_PAUSE_PROCESS = 2 --- The
Tweenwill process regardless of whether SceneTree is paused.
TRANS_LINEAR = 0 --- The animation is interpolated linearly.
TRANS_SINE = 1 --- The animation is interpolated using a sine function.
TRANS_QUINT = 2 --- The animation is interpolated with a quintic (to the power of 5) function.
TRANS_QUART = 3 --- The animation is interpolated with a quartic (to the power of 4) function.
TRANS_QUAD = 4 --- The animation is interpolated with a quadratic (to the power of 2) function.
TRANS_EXPO = 5 --- The animation is interpolated with an exponential (to the power of x) function.
TRANS_ELASTIC = 6 --- The animation is interpolated with elasticity, wiggling around the edges.
TRANS_CUBIC = 7 --- The animation is interpolated with a cubic (to the power of 3) function.
TRANS_CIRC = 8 --- The animation is interpolated with a function using square roots.
TRANS_BOUNCE = 9 --- The animation is interpolated by bouncing at the end.
TRANS_BACK = 10 --- The animation is interpolated backing out at ends.
EASE_IN = 0 --- The interpolation starts slowly and speeds up towards the end.
EASE_OUT = 1 --- The interpolation starts quickly and slows down towards the end.
Tween with the given
Tweens are processed directly by the SceneTree, so they run independently of the animated nodes. When you bind a Node with 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 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 Node.create_tween.
Tween chain ( )
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.
Tween by given
delta 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
delta longer than the whole duration.
true if the
Tween still has Tweeners that haven't finished.
Tween will become invalid after finished, but you can call stop after the step, to keep it and reset.
float get_total_elapsed_time ( ) const
Note: As it results from accumulating frame deltas, the time returned after the
Tween has finished animating will be slightly greater than the actual
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 @GlobalScope.lerp, but with support for custom transition and easing.
initial_value is the starting value of the interpolation.
delta_value is the change of the value in the interpolation, i.e. it's equal to
final_value - initial_value.
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
duration, the interpolated value will be halfway between initial and final values. This value can also be greater than
duration or lower than 0, which will extrapolate the value.
duration is the total time of the interpolation.
duration is equal to
0, the method will always return the final value, regardless of
bool is_running ( )
Returns whether the
Tween is currently running, i.e. it wasn't paused and it's not finished.
bool is_valid ( )
Returns whether the
Tween is valid. A valid
Tween is a
Tween contained by the scene tree (i.e. the array from SceneTree.get_processed_tweens will contain this
Tween might become invalid when it has finished tweening or was killed, also when created with
Tween can't have Tweeners appended, because it can't animate them.
void kill ( )
Aborts all tweening operations and invalidates the
Tween parallel ( )
Makes the next Tweener run parallelly to the previous one. Example:
var tween = create_tween() tween.tween_property(...) tween.parallel().tween_property(...) tween.parallel().tween_property(...)
All Tweeners in the example will run at the same time.
You can make the
Tween parallel by default by using set_parallel.
void pause ( )
Pauses the tweening. The animation can be resumed by using play.
void play ( )
Resumes a paused or stopped
Sets the number of times the tweening sequence will be repeated, i.e.
set_loops(2) will run the animation twice.
Calling this method without arguments will make the
Tween run infinitely, until it is either killed by kill or by freeing bound node, or all the animated objects have been freed (which makes further animation impossible).
Warning: Make sure to always add some duration/delay when using infinite loops. 0-duration looped animations (e.g. single CallbackTweener with no delay or PropertyTweener with invalid node) are equivalent to infinite
while loops and will freeze your game. If a
Tween's lifetime depends on some node, always use bind_node.
true, the Tweeners appended after this method will by default run simultaneously, as opposed to sequentially.
Default value is TWEEN_PAUSE_BOUND.
Default value is TWEEN_PROCESS_IDLE.
Scales the speed of tweening. This affects all Tweeners and their delays.
void stop ( )
Stops the tweening and resets the
Tween to its initial state. This will not remove any appended Tweeners.
Example: object that keeps shooting every 1 second.
var tween = get_tree().create_tween().set_loops() tween.tween_callback(shoot).set_delay(1)
Example: turning a sprite red and then blue, with 2 second delay.
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)
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 Tweeners or when there's no animation (in which case the
Tween acts as a timer).
time is the length of the interval, in seconds.
Example: creating an interval in code execution.
# ... some code await create_tween().tween_interval(2).finished # ... more code
Example: creating an object that moves back and forth and jumps every few seconds.
var tween = create_tween().set_loops() tween.tween_property($Sprite, "position:x", 200.0, 1).as_relative() tween.tween_callback(jump) tween.tween_interval(2) tween.tween_property($Sprite, "position:x", -200.0, 1).as_relative() tween.tween_callback(jump) tween.tween_interval(2)
Creates and appends a MethodTweener. This method is similar to a combination of tween_callback and tween_property. It calls a method over time with a tweened value provided as an argument. The value is tweened between
to over the time specified by
duration, in seconds. Use Callable.bind to bind additional arguments for the call. You can use MethodTweener.set_ease and MethodTweener.set_trans to tweak the easing and transition of the value or MethodTweener.set_delay to delay the tweening.
Example: making a 3D object look from one point to another point.
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.
Example: setting a text of a Label, using an intermediate method and after a delay.
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)
Creates and appends a PropertyTweener. This method tweens a
property of an
object between an initial value and
final_val in a span of time equal to
duration, in seconds. The initial value by default is a value at the time the tweening of the PropertyTweener start. For example:
var tween = create_tween() tween.tween_property($Sprite, "position", Vector2(100, 200), 1) tween.tween_property($Sprite, "position", Vector2(200, 300), 1)
will move the sprite to position (100, 200) and then to (200, 300). If you use PropertyTweener.from or 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.
Note: 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
position:x), where it would only apply to that particular component.
Example: moving object twice from the same position, with different transition types.
var tween = create_tween() tween.tween_property($Sprite, "position", Vector2.RIGHT * 300, 1).as_relative().set_trans(Tween.TRANS_SINE) tween.tween_property($Sprite, "position", Vector2.RIGHT * 300, 1).as_relative().from_current().set_trans(Tween.TRANS_EXPO)