How to increase the length of AnimationPlayer via script?

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

Greets!

Meaning i got a button wich once pressed should increase it by some minutes.
Cheers!

:bust_in_silhouette: Reply From: Zylann

I guess you can assign the length property of the Animation resource?

Not really cause i want it to increase it from undefined times, for undefined times. Not precise key. That is, my button pressed at a certain moment in time (it’s a day/night animation), will increase it of say, 2 hours. I cannot fix a key for that.

Unless set_length(+value) is doable?

Syl | 2020-02-25 02:13

length += some_value works too, however you mentionned day/night cycle so I don’t know if that’s really what you need.
You’ll have to explain better what you want to achieve, because it’s really vague.

Zylann | 2020-02-25 18:38

Actually, i just want my button pressed to increase the length of the animationplayer. something like that:

if button_pressed:
       animationplayer.length += 50 

but that’s an invalid index ‘length’…

Syl | 2020-02-25 19:58

As I mentionned earlier, length is a property of Animation, not AnimationPlayer. The latter just plays the animation.

You could try this:

var anim = animationplayer.get_animation(animationplayer.current_animation)
anim.length += 50

Note: It will also remain with that length until you reset it, so you may want to keep the original value in a variable.
If that doesn’t work well, you could also try to pause the animation player instead and use a timer to count the extra time (because increasing length won’t add any keys, so will have same outcome as pausing for a period of time).

Zylann | 2020-02-25 20:03

Sry,the best i could think of:

extends AnimationPlayer
onready var animationplayer = get_node("/root/Node2D/Timer/AnimationPlayer")
var anim = animationplayer.get_animation(animationplayer.current_animation)

func _on_Hunt_pressed():
	anim.length += 50

AnimationPlayer conected by Hunt button, and i got ‘invalid get index ‘current_animation’ (on base nil)’ error. Then i don’t know. Maybe i’m still too low on godot skill to tackle that for now…

Syl | 2020-02-25 23:04

Don’t put this outside. It’s taking the current anim, and no anim plays yet when member variables are initialized.
Place it inside the function:

func _on_Hunt_pressed():
	var anim = animationplayer.get_animation(animationplayer.current_animation)
	anim.length += 50

Zylann | 2020-02-25 23:40

‘The identifier ‘animationplayer’ isn’t declared in the current scope’ now…

Syl | 2020-02-26 01:18

Ok here is the entire script… I just moved anim in the function

extends AnimationPlayer
onready var animationplayer = get_node("/root/Node2D/Timer/AnimationPlayer")

func _on_Hunt_pressed():
	var anim = animationplayer.get_animation(animationplayer.current_animation)
    anim.length += 50

Zylann | 2020-02-26 01:20

My bad, so sorry. I asked you to increase the animation length, when i wanted to increase the current second/position. That is, if the animation is at 30 sec when the button is pressed, it comes at 80…

I’m so sorry. :c

Syl | 2020-02-26 01:38