How to set animation length through code? (AnimationPlayer)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GunPoint

I need to change an animation’s length through code. I also want the keys to be scalable. I haven’t found any methods for changing an animation’s length. Can I do that?

:bust_in_silhouette: Reply From: timCopwell

Length of your animation should be set within animation node, not the player. Try something like this.

var length = 3.0

var    your_animation = Animation.new()
your_animation.add_track(0)
animation_float.track_set_path(0, "Sprite:position")
your_animation.track_insert_key ( 0, 0, Vector2(0, 0))
your_animation.track_insert_key ( 0, length, Vector2(128, 0))

Basicly you create custom length variable and assign it to time argument when inserting new key to track. With continous updateMode it should work with any amout of frames, if Im not mistaken.

After animation is set, you add it to your player by add_animation() function.

Have a research here: Animation — Godot Engine (3.0) documentation in English

:bust_in_silhouette: Reply From: Roi Valcárcel

You can change the speed of animation:

var speed = 0.5
$AnimationNode.playback_speed = speed

If the original Animation length is 5 seconds and you change speed to 0.5, the animation will play 10 seconds (half speed). If you change speed to 2, the animation will play at double speed and the lenght will be 2.5 seconds. 1 is for normal speed.

:bust_in_silhouette: Reply From: Dlean Jeans
$AnimationPlayer.get_animation('YourAnimation').length = new_length

Hahah I tried get_current_animation().get_length() but it didn’t worked. However, get_animation("").length worked. Thank you

GunPoint | 2019-02-07 09:28

try $AnimationPlayer.current_animation_length

luislodosm | 2020-10-01 18:01