AnimationPlayer laggy when editing animation from code

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

Hello there!

In my game I have a camera that is turns X degrees to the left, then X degrees to the right and repeat that. It rotates using an AnimationPlayer.

I’m trying to edit the camera rotation from code using an exported variable, so I can easily change it.I’m trying to do that using the following code:

extends Node2D

export(int) var max_rotation = 30

func _ready():
	var animation = $AnimationPlayer.get_animation("rotate")
	
	animation.track_set_key_value(0, 1, -max_rotation)
	animation.track_set_key_value(0, 2, -max_rotation)
	animation.track_set_key_value(0, 3, max_rotation)
	animation.track_set_key_value(0, 4, max_rotation)

It works but the animation becomes laggy (see this video). The first part of the animation, where it’s not laggy, is when it’s using the original animation value (if you did not notice, I changed the keys from 1 to 4, did not change the 0 key).

I already tried using float instead of int in the max_rotation and also tried adding the keys (using an empty animation) instead of editing their values but I got the same result. Also tried setting the autoplay on load in code instead of setting in the AnimationPlayer but the result was the same.

I ran out of ideas, can someone help me to get this fixed?

If you would like to test on your computer, here is the project on GitHub.