Halting playback when adding frames to SpriteFrames of AnimatedSprite2D during "runtime"

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 a AnimatedSprite2D node.
  • I’ve created a SpriteFrames resource for the AnimatedSprite2D.
  • 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 display print() 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:

godot-animatedsprite2d-anim_lag-2

And this is the GIF of the animation that was added manually in the Editor.
godot-animatedsprite2d-anim_lag-1

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.

After posting this, I created an issue in the Godot GH and created a repro project. After posting the issue there, I made a workaround discovery.

It seems that SpriteFrames.add_frames does not like adding the at_position argument:

void add_frame ( String anim, Texture frame, int at_position=-1 )

When I omit passing a value to at_position, the bug/lagginess goes away.

sprite_frames.add_frame(anim_name, new_texture)

This is still a bug, obviously. But I’m thankful there is a workaround for now.

Apparently, for future readers: this was not a bug at all. add_frames in Godot4 now has 4 parameters, and the 3rd parameter was duration, not frame index as I suspected. Sorry for the noise.