Godot Version
Gogot v4.2.2.stable
Question
Hi, I’m having issues with stuttering animation playback in AnimatedSprite2D when I try to dynamically populate the SpriteFrames library with new animation from code.
This is my initial setup:
- I have several PNG animation sequences with 11 frames each.
- The resolution of these PNGs vary, but they average to 96x150px (i.e. not very big).
- These PNG animation sequences have been copied into the Godot project, and subsequently imported by Godot; it has an
.import
file. - I have a
Node2D
in the scene. Under that is aAnimatedSprite2D
node. - I’ve created a
SpriteFrames
resource for theAnimatedSprite2D
. - In the
SpriteFrames
library, I have created a “dummy” animation set called “default”.
Now, my intention is to create my own animation sets in SpriteFrames, by using ResourceLoader.load
, and adding frames using SpriteFrames.add_frame
.
This is working OK.
sprite_frames.add_animation(anim_name)
...
if ResourceLoader.exists(res_file):
var new_texture = ResourceLoader.load(res_file)
sprite_frames.add_frame(anim_name, new_texture, ndx)
I can tell it works because I am able to play back the animation.
But the problem is that the frame lags, which means it plays back in an inconsistent rate, and is actually slower than the frame rate that was set.
- I have ensured that
SpriteFrames.animation_speed
is set to 12 (fps) AnimatedSprite2D.speed_scale
is 1- I must also note that it is not framerate lag issue because I can move my Node2D smoothly around.
- Also, printing out out the current frame (i.e.
AnimatedSprite2D.get_frame()
) in_process
shows no delay in displayprint()
but I can see that the frame number is not advancing normally.
So I thought that I should test the difference between adding frames in runtime (i.e. in code) vs adding frames in the SpriteFrames Editor.
- I created on one set in SpriteFrames Editor using one of the PNG sequences,
- Other PNG sequences were still dynamically added.
The result was playback was smooth on the animation that was added in the Editor and the animation lagged on the animation that was added in code.
This is a a GIF showing the laggy animation:
And this is the GIF of the animation that was added manually in the Editor.
By the way, I know this wasn’t the case in Godot 3, because this is the same method I was using for a prototype. It’s just now that I’ve discovered this. I’m wondering if anyone has seen this before, and if there were any workarounds or something that I have missed, since I’m new to v4.
Thanks for reading, and I hope I can get some help re this.