SceneTreeTween
Inherits: Reference < Object Tweeners.
Description
SceneTreeTween is a tween managed by the scene tree. As opposed to Tween, it does not require the instantiation of a node.
SceneTreeTweens are 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.
SceneTreeTween can be created by using either SceneTree.create_tween or Node.create_tween. SceneTreeTweens created manually (i.e. by using Tween.new()) are invalid. They can’t be used for tweening values, but you can do manual interpolation with interpolate_value.
Tweeners to the SceneTreeTween object, using tween_property, tween_interval, tween_callback or 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")
$Sprite node turn red, then shrink, before finally calling Node.queuefree to free the sprite. Tweeners are executed one after another by default. This behavior can be changed using parallel and set_parallel.
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 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")
SceneTreeTween methods can be chained this way too. In the following example the SceneTreeTween 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")
SceneTreeTweens 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).
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: All SceneTreeTweens will automatically start by default. To prevent a SceneTreeTween from autostarting, you can call stop immediately after it is created.
Methods
Signals
- finished ( )
SceneTreeTweenhas finished all tweening. Never emitted when theSceneTreeTweenis set to infinite looping (see set_loops). Note: TheSceneTreeTweenis removed (invalidated) in the next processing frame after this signal is emitted. Calling stop inside the signal callback will prevent theSceneTreeTweenfrom being removed.
- loop_finished ( int loop_count ) set_loops), providing the loop index. This signal is not emitted after the final loop, use finished instead for this case.
- step_finished ( int idx )
SceneTreeTweenis complete, providing the step index. One step is either a single Tweener or a group of Tweeners running in parallel.Enumerations
TweenPauseMode: - TWEEN_PAUSE_BOUND = 0 —- If the
SceneTreeTweenhas a bound node, it will process when that node can process (see Node.pause_mode). Otherwise it’s the same as TWEEN_PAUSE_STOP. - TWEEN_PAUSE_STOP = 1 —- If SceneTree is paused, the
SceneTreeTweenwill also pause. - TWEEN_PAUSE_PROCESS = 2 —- The
SceneTreeTweenwill process regardless of whether SceneTree is paused.Method Descriptions
- SceneTreeTween bind_node ( Node node )
SceneTreeTweenwith the givennode.SceneTreeTweens are processed directly by the SceneTree, so they run independently of the animated nodes. When you bind a Node with theSceneTreeTween, theSceneTreeTweenwill halt the animation when the object is not inside tree and theSceneTreeTweenwill be automatically killed when the bound object is freed. Also TWEEN_PAUSE_BOUND will make the pausing behavior dependent on the bound node.SceneTreeTween, you can use Node.create_tween.
- SceneTreeTween chain ( )
Tweeners after set_parallel is called with
true.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.
- bool custom_step ( float delta )
SceneTreeTweenby the givendeltavalue, in seconds. This is mostly useful for manual control when theSceneTreeTweenis paused. It can also be used to end theSceneTreeTweenanimation immediately, by settingdeltalonger than the whole duration of theSceneTreeTweenanimation.trueif theSceneTreeTweenstill has Tweeners that haven’t finished. Note: TheSceneTreeTweenwill become invalid in the next processing frame after its animation finishes. Calling stop after performing custom_step instead keeps and resets theSceneTreeTween.
- float get_total_elapsed_time ( ) const
SceneTreeTweenhas been animating (i.e. the time since it started, not counting pauses etc.). The time is affected by set_speed_scale, and stop will reset it to0. Note: As it results from accumulating frame deltas, the time returned after theSceneTreeTweenhas finished animating will be slightly greater than the actualSceneTreeTweenduration.
- Variant interpolate_value ( Variant initial_value, Variant delta_value, float elapsed_time, float duration, TransitionType trans_type, EaseType ease_type ) const
SceneTreeTweento do animating for you. It’s similar to @GDScript.lerp, but with support for custom transition and easing.initial_valueis the starting value of the interpolation.delta_valueis the change of the value in the interpolation, i.e. it’s equal tofinal_value - initial_value.elapsed_timeis 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 theduration, the interpolated value will be halfway between initial and final values. This value can also be greater thandurationor lower than 0, which will extrapolate the value.durationis the total time of the interpolation. Note: Ifdurationis equal to0, the method will always return the final value, regardless ofelapsed_timeprovided.
- bool is_running ( ) const
SceneTreeTweenis currently running, i.e. it wasn’t paused and it’s not finished.
- bool is_valid ( ) const
SceneTreeTweenis valid. A validSceneTreeTweenis aSceneTreeTweencontained by the scene tree (i.e. the array from SceneTree.get_processed_tweens will contain thisSceneTreeTween). ASceneTreeTweenmight become invalid when it has finished tweening, is killed, or when created withSceneTreeTween.new(). InvalidSceneTreeTweens can’t have Tweeners appended. You can however still use interpolate_value.
- kill ( )
SceneTreeTween.
- SceneTreeTween parallel ( )
Tweener run parallelly to the previous one. Example:
Tweeners in the example will run at the same time.var tween = create_tween()tween.tween_property(...)tween.parallel().tween_property(...)tween.parallel().tween_property(...)
SceneTreeTweenparallel by default by using set_parallel.
- pause ( ) play.
- play ( )
SceneTreeTween.
- SceneTreeTween set_ease ( EaseType ease )
PropertyTweeners and MethodTweeners animated by this
SceneTreeTween.
- SceneTreeTween set_loops ( int loops=0 )
set_loops(2)will run the animation twice.SceneTreeTweenrun infinitely, until either it is killed with kill, theSceneTreeTween‘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 aSceneTreeTween‘s lifetime depends on some node, always use bind_node.
- SceneTreeTween set_parallel ( bool parallel=true )
parallelistrue, the Tweeners appended after this method will by default run simultaneously, as opposed to sequentially.
- SceneTreeTween set_pause_mode ( TweenPauseMode mode )
SceneTreeTweenwhen the SceneTree is paused. Check TweenPauseMode for options. TWEEN_PAUSE_BOUND.
- SceneTreeTween set_process_mode ( TweenProcessMode mode )
SceneTreeTweenshould run during idle frame (see Node._process) or physics frame (see Node._physics_process. Tween.TWEEN_PROCESS_IDLE.
- SceneTreeTween set_speed_scale ( float speed ) Tweeners and their delays.
- SceneTreeTween set_trans ( TransitionType trans )
PropertyTweeners and MethodTweeners animated by this
SceneTreeTween.
- stop ( )
SceneTreeTweento its initial state. This will not remove any appended Tweeners.
- CallbackTweener tween_callback ( Object object, String method, Array binds=[ ] )
CallbackTweener. This method can be used to call an arbitrary method in any object. Use
bindsto bind additional arguments for the call. Example: object that keeps shooting every 1 second.
Example: turning a sprite red and then blue, with 2 second delay.var tween = get_tree().create_tween().set_loops()tween.tween_callback(self, "shoot").set_delay(1)
var tween = get_tree().create_tween()tween.tween_callback($Sprite, "set_modulate", [Color.red]).set_delay(2)tween.tween_callback($Sprite, "set_modulate", [Color.blue]).set_delay(2)
- IntervalTweener tween_interval ( float time )
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
SceneTreeTweenacts as a timer).timeis 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.# ... some codeyield(create_tween().tween_interval(2), "finished")# ... more code
var tween = create_tween().set_loops()tween.tween_property($Sprite, "position:x", 200.0, 1).as_relative()tween.tween_callback(self, "jump")tween.tween_interval(2)tween.tween_property($Sprite, "position:x", -200.0, 1).as_relative()tween.tween_callback(self, "jump")tween.tween_interval(2)
- MethodTweener tween_method ( Object object, String method, Variant from, Variant to, float duration, Array binds=[ ] )
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
fromandtoover the time specified byduration, in seconds. Usebindsto 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.
Label, using an intermediate method and after a delay.var tween = create_tween()tween.tween_method(self, "look_at", Vector3(-1, 0, -1), Vector3(1, 0, -1), 1, [Vector3.UP]) # The look_at() method takes up vector as second argument.
func _ready(): var tween = create_tween() tween.tween_method(self, "set_label_text", 0, 10, 1).set_delay(1)func set_label_text(value: int): $Label.text = "Counting " + str(value)
- PropertyTweener tween_property ( Object object, NodePath property, Variant final_val, float duration )
PropertyTweener. This method tweens a
propertyof anobjectbetween an initial value andfinal_valin a span of time equal toduration, in seconds. The initial value by default is the property’s value at the time the tweening of the PropertyTweener starts. For example:
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 usingvar tween = create_tween()tween.tween_property($Sprite, "position", Vector2(100, 200), 1)tween.tween_property($Sprite, "position", Vector2(200, 300), 1)
"property:component"(eg.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)
