• 2D
  • [Answered] Change animation track key time by code

I don't seem able to set the key time through code. I get the key time but not set it.

Here's what I'm attempting. Player starts each level at dawn and time passes until darkness. The background modulates gradually through the colors via an animation. What I would like to do is randomly set the track length, which I can do but it doesn't change the key length/step. Essentially it just truncates the track. Is this possible?

I don’t know if you can change the key length/step, but you might be able to get around it by changing the speed property of the AnimationPlayer.

How you would calculate the value to set speed to I don’t know right off, but in theory you’d just need to find out where the start of the animation is, convert that to a 1-0 range and then set the speed property according.

That way, if you start half way through the animation the speed property will be 0.5 and the animation will take the same amount of time as it would if you started at the beginning with a speed of 1.

Hopefully this helps :smile:

Edit: Sorry, the property is playback_speed not speed, according to the documentation.

that's a great idea! I never thought of speeding up/down the whole track. =)

Didn't work. If the track is 60 seconds, setting it to 30 seconds essentially halves the entire thing, not shrink everything by 50%, i.e. I get half the whole animation :(

@nathanjwtx said: Didn't work. If the track is 60 seconds, setting it to 30 seconds essentially halves the entire thing, not shrink everything by 50%, i.e. I get half the whole animation :(

You shouldn't need to the actual length of the animation, just how fast it plays. If you select the AnimationPlayer node, you can edit the speed field:

Changing the value in speed will change how fast the animation plays. For example, in the image above it is playing at 1/4 the actual speed of the animation. The animation in the example (the Gui_in_3D demo) is 6 seconds long, so playing it at a quarter of the speed will make the animation last 24 seconds.


The hard part in your case is figuring out what to set playback_speed (or speed in the inspector) to.

I think the algorithm for figuring out the speed should be this (untested):

animation_player.speed = 1 - (animation_position / animation_length);

So as long as you know how long your animation is, and where you are starting to play the animation, in theory you should be able to set the speed of the AnimationPlayer node so that the animation always lasts however many seconds it is supposed to.

Granted this will make it where depending on where the animation starts, the actual animation will appear to move at different speeds, but will always end in the same length of time.

Using the example of fading to black, it will always fade to black in N many seconds. However if it starts near the end as is more black, it will seem to very slowly fade to black. In contrast, if it starts at the beginning, it will seem to fade faster.

If you need, I can probably make a quick example project sometime in the next couple days to show what I mean.

Hopefully this helps!

I tried again this morning and I can safely conclude that beer drinking and coding don't go hand in hand! =)

I was using the property, as you pointed out. Changing the speed does in fact do what I want.

Thanks again!

@nathanjwtx said: I tried again this morning and I can safely conclude that beer drinking and coding don't go hand in hand! =)

I was using the property, as you pointed out. Changing the speed does in fact do what I want.

Thanks again!

Great! I'm glad it works! :smiley:

4 days later

Changed the topic type into a question since you indicated interest in that.