Class Tween
Lightweight object used for general-purpose animation via script, using Tweeners.
- Inheritance
-
Tween
Remarks
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. Animating something with a Tween is called tweening.
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 create_tween or create_tween. Tweens created manually (i.e. by using Tween.new()
) are invalid and can't be used for tweening values.
A tween animation is created by adding Tweeners to the Tween object, using Tween.tween_property, Tween.tween_interval, Tween.tween_callback or Tween.tween_method:
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, before finally calling queue_free to free the sprite. Tweeners are executed one after another by default. This behavior can be changed using parallel and Tween.set_parallel.
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 a different transition type in the above example, you can use Tween.set_trans:
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 the following example the Tween is bound to the running script's node and a default transition is set for its Tweeners:
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 sets of objects:
var tween = create_tween()
for sprite in get_children():
tween.tween_property(sprite, "position", Vector2(0, 0), 1)
In the example above, all children of a node are moved one after another to position (0, 0).
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:
var tween
func animate():
if tween:
tween.kill() # Abort the previous animation.
tween = create_tween()
Some Tweeners use transitions and eases. The first accepts a 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 Tween.EASE_IN_OUT, and use the one that looks best.
\ Tween easing and transition types cheatsheet\
Note: 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.
Note: The tween is processed after all of the nodes in the current frame, i.e. node's Node._process method would be called before the tween (or Node._physics_process depending on the value passed to Tween.set_process_mode).
Methods
bind_node(Node)
Binds this Tween with the given node
. Tweens 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 Tween.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 create_tween.
Tween bind_node(Node node)
Parameters
node
Node
chain
Used to chain two Tweeners after Tween.set_parallel is called with true
.
Tween chain
custom_step(float)
Processes the Tween by the given 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 delta
longer than the whole duration of the Tween animation.
Returns true
if the Tween still has Tweeners that haven't finished.
bool custom_step(float delta)
Parameters
delta
float
get_loops_left
Qualifiers: const
Returns the number of remaining loops for this Tween (see Tween.set_loops). A return value of -1
indicates an infinitely looping Tween, and a return value of 0
indicates that the Tween has already finished.
int get_loops_left
get_total_elapsed_time
Qualifiers: const
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 Tween.set_speed_scale, and stop will reset it to 0
.
Note: 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.
float get_total_elapsed_time
interpolate_value(Variant, Variant, float, float, int, int)
Qualifiers: static
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.
Note: If duration
is equal to 0
, the method will always return the final value, regardless of elapsed_time
provided.
Variant interpolate_value(Variant initial_value, Variant delta_value, float elapsed_time, float duration, int trans_type, int ease_type)
Parameters
initial_value
Variantdelta_value
Variantelapsed_time
floatduration
floattrans_type
intease_type
int
is_running
Returns whether the Tween is currently running, i.e. it wasn't paused and it's not finished.
bool is_running
is_valid
Returns whether the Tween is valid. A valid Tween is a Tween contained by the scene tree (i.e. the array from get_processed_tweens will contain this Tween). A Tween might become invalid when it has finished tweening, is killed, or when created with Tween.new()
. Invalid Tweens can't have Tweeners appended.
bool is_valid
kill
Aborts all tweening operations and invalidates the Tween.
void kill
parallel
Makes the next Tweener run parallelly to the previous one.
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 Tween.set_parallel.
Tween parallel
pause
Pauses the tweening. The animation can be resumed by using play.
Note: 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 get_processed_tweens.
void pause
play
Resumes a paused or stopped Tween.
void play
set_ease(int)
Sets the default ease type for PropertyTweeners and MethodTweeners appended after this method.
Before this method is called, the default ease type is Tween.EASE_IN_OUT.
var tween = create_tween()
tween.tween_property(self, "position", Vector2(300, 0), 0.5) # Uses EASE_IN_OUT.
tween.set_ease(Tween.EASE_IN)
tween.tween_property(self, "rotation_degrees", 45.0, 0.5) # Uses EASE_IN.
Tween set_ease(int ease)
Parameters
ease
int
set_ignore_time_scale(bool)
If ignore
is true
, the tween will ignore time_scale and update with the real, elapsed time. This affects all Tweeners and their delays. Default value is false
.
Tween set_ignore_time_scale(bool ignore)
Parameters
ignore
bool
set_loops(int)
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 either it is killed with kill, the Tween's bound node is freed, 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. 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 Tween.bind_node.
Tween set_loops(int loops)
Parameters
loops
int
set_parallel(bool)
If parallel
is true
, the Tweeners appended after this method will by default run simultaneously, as opposed to sequentially.
Note: Just like with parallel, the tweener added right before this method will also be part of the parallel step.
tween.tween_property(self, "position", Vector2(300, 0), 0.5)
tween.set_parallel()
tween.tween_property(self, "modulate", Color.GREEN, 0.5) # Runs together with the position tweener.
Tween set_parallel(bool parallel)
Parameters
parallel
bool
set_pause_mode(int)
Determines the behavior of the Tween when the SceneTree is paused. Check TweenPauseMode for options.
Default value is Tween.TWEEN_PAUSE_BOUND.
Tween set_pause_mode(int mode)
Parameters
mode
int
set_process_mode(int)
Determines whether the Tween should run after process frames (see Node._process) or physics frames (see Node._physics_process).
Default value is Tween.TWEEN_PROCESS_IDLE.
Tween set_process_mode(int mode)
Parameters
mode
int
set_speed_scale(float)
Scales the speed of tweening. This affects all Tweeners and their delays.
Tween set_speed_scale(float speed)
Parameters
speed
float
set_trans(int)
Sets the default transition type for PropertyTweeners and MethodTweeners appended after this method.
Before this method is called, the default transition type is Tween.TRANS_LINEAR.
var tween = create_tween()
tween.tween_property(self, "position", Vector2(300, 0), 0.5) # Uses TRANS_LINEAR.
tween.set_trans(Tween.TRANS_SINE)
tween.tween_property(self, "rotation_degrees", 45.0, 0.5) # Uses TRANS_SINE.
Tween set_trans(int trans)
Parameters
trans
int
stop
Stops the tweening and resets the Tween to its initial state. This will not remove any appended Tweeners.
Note: This does not reset targets of PropertyTweeners to their values when the Tween first started.
var tween = create_tween()
# Will move from 0 to 500 over 1 second.
position.x = 0.0
tween.tween_property(self, "position:x", 500, 1.0)
# Will be at (about) 250 when the timer finishes.
await get_tree().create_timer(0.5).timeout
# Will now move from (about) 250 to 500 over 1 second,
# thus at half the speed as before.
tween.stop()
tween.play()
Note: 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 get_processed_tweens.
void stop
tween_callback(Callable)
Creates and appends a CallbackTweener. This method can be used to call an arbitrary method in any object. Use Callable.bind to bind additional arguments for the call.
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:
CallbackTweener tween_callback(Callable callback)
Parameters
callback
Callable
tween_interval(float)
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 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:
Example: Creating an object that moves back and forth and jumps every few seconds:
IntervalTweener tween_interval(float time)
Parameters
time
float
tween_method(Callable, Variant, Variant, float)
Creates and appends a MethodTweener. This method is similar to a combination of Tween.tween_callback and Tween.tween_property. It calls a method over time with a tweened value provided as an argument. The value is tweened between from
and 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 the text of a Label, using an intermediate method and after a delay:
MethodTweener tween_method(Callable method, Variant from, Variant to, float duration)
Parameters
tween_property(Object, NodePath, Variant, float)
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 the property's value at the time the tweening of the PropertyTweener starts.
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 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 "property:component"
(eg. position:x
), where it would only apply to that particular component.
Example: Moving an object twice from the same position, with different transition types:
PropertyTweener tween_property(Object object, NodePath property, Variant final_val, float duration)
Parameters
tween_subtween(Tween)
Creates and appends a SubtweenTweener. This method can be used to nest subtween
within this Tween, allowing for the creation of more complex and composable sequences.
# Subtween will rotate the object.
var subtween = create_tween()
subtween.tween_property(self, "rotation_degrees", 45.0, 1.0)
subtween.tween_property(self, "rotation_degrees", 0.0, 1.0)
# Parent tween will execute the subtween as one of its steps.
var tween = create_tween()
tween.tween_property(self, "position:x", 500, 3.0)
tween.tween_subtween(subtween)
tween.tween_property(self, "position:x", 300, 2.0)
Note: The methods pause, stop, and Tween.set_loops can cause the parent Tween to get stuck on the subtween step; see the documentation for those methods for more information.
Note: The pause and process modes set by Tween.set_pause_mode and Tween.set_process_mode on subtween
will be overridden by the parent Tween's settings.
SubtweenTweener tween_subtween(Tween subtween)
Parameters
subtween
Tween
Events
finished
Emitted when the Tween has finished all tweening. Never emitted when the Tween is set to infinite looping (see Tween.set_loops).
signal finished
loop_finished(int)
Emitted when a full loop is complete (see Tween.set_loops), providing the loop index. This signal is not emitted after the final loop, use finished instead for this case.
signal loop_finished(int loop_count)
Parameters
loop_count
int
step_finished(int)
Emitted when one step of the Tween is complete, providing the step index. One step is either a single Tweener or a group of Tweeners running in parallel.
signal step_finished(int idx)
Parameters
idx
int