Skip to content

Always call set_animation in set_sprite_frames#86277

Open
rileylyman wants to merge 1 commit into
godotengine:masterfrom
rileylyman:riley-bug-first-anim-frame
Open

Always call set_animation in set_sprite_frames#86277
rileylyman wants to merge 1 commit into
godotengine:masterfrom
rileylyman:riley-bug-first-anim-frame

Conversation

@rileylyman
Copy link
Copy Markdown
Contributor

Fixes #86058.

Before this patch, say that an AnimatedSprite2D called a currently has the active animation of "foo". If we update a's sprite frames and the new sprite frames also have an animation called "foo", then a will not call set_animation("foo") and so will continue to execute the new "foo" animation as if it was the original "foo" animation. It will continue playing from the same frame with the same speed, even if the new "foo" has fewer frames or should last for a different duration.

This manifested in #86058 with the "default" animation. The first frame of the "default" animation in this case was supposed to last for a duration of 10, but only lasted for 1 since we never called set_animation("default") with the new sprite frames and so the AnimatedSprite2D just used its default values until the next frame.

I also think we could get the engine to error by writing some sort of script that swapped between two sets of sprite frames with the same animation name but different frame lengths, but I didn't try it. This change would fix that as well.

@rileylyman rileylyman requested a review from a team as a code owner December 17, 2023 23:05
@Calinou Calinou added bug topic:animation topic:2d cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release labels Dec 17, 2023
@Calinou Calinou added this to the 4.3 milestone Dec 17, 2023
Comment thread scene/2d/animated_sprite_2d.cpp Outdated
@fire fire changed the title always call set_animation in set_sprite_frames Always call set_animation in set_sprite_frames Dec 18, 2023
@rileylyman rileylyman force-pushed the riley-bug-first-anim-frame branch from 6772b0c to ed4b82c Compare December 18, 2023 19:59
@rileylyman rileylyman requested a review from dalexeev December 18, 2023 20:00
@dalexeev
Copy link
Copy Markdown
Member

dalexeev commented Dec 19, 2023

The changes look good to me. Please make the same changes to AnimatedSprite3D::set_sprite_frames() and correct the commit message (it must start with a capital letter, see CONTRIBUTING.md).

https://github.com/godotengine/godot/blob/ed4b82c665221b6fd3c8629d563dfb22be02fa19/scene/3d/sprite_3d.cpp#L1113-L1125

@rileylyman rileylyman force-pushed the riley-bug-first-anim-frame branch from ed4b82c to 5ad4df1 Compare December 19, 2023 19:46
@rileylyman rileylyman requested a review from a team as a code owner December 19, 2023 19:46
Copy link
Copy Markdown
Member

@AThousandShips AThousandShips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaner and avoids using index on a list (even though it's equivalent it's better to avoid it)


animation = StringName();
if (!al.is_empty()) {
set_animation(frames->has_animation(animation) ? animation : al[0]);
Copy link
Copy Markdown
Member

@AThousandShips AThousandShips Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set_animation(frames->has_animation(animation) ? animation : al[0]);
set_animation(frames->has_animation(animation) ? animation : *al.front());

Comment thread scene/3d/sprite_3d.cpp

animation = StringName();
if (!al.is_empty()) {
set_animation(frames->has_animation(animation) ? animation : al[0]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set_animation(frames->has_animation(animation) ? animation : al[0]);
set_animation(frames->has_animation(animation) ? animation : al.front());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it, it turned out I needed al.front()->get() instead, which I find a lot less clear and the same amount of scary since it requires a ptr deref, so i would prefer to keep al[0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can instead do:

Suggested change
set_animation(frames->has_animation(animation) ? animation : al[0]);
set_animation(frames->has_animation(animation) ? animation : *al.front());

Which should be clearer, but it's either way 🙂

@KoBeWi KoBeWi modified the milestones: 4.3, 4.4 Aug 3, 2024
@KoBeWi KoBeWi added the cherrypick:4.3 Considered for cherry-picking into a future 4.3.x release label Aug 3, 2024
@Repiteo Repiteo added the cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release label Feb 24, 2025
@Repiteo Repiteo modified the milestones: 4.4, 4.5 Feb 24, 2025
@akien-mga akien-mga removed the cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release label Mar 19, 2025
@Repiteo Repiteo added the cherrypick:4.5 Considered for cherry-picking into a future 4.5.x release label Sep 5, 2025
@Repiteo Repiteo modified the milestones: 4.5, 4.6 Sep 5, 2025
@Repiteo Repiteo modified the milestones: 4.6, 4.x Jan 26, 2026
@Repiteo Repiteo removed the cherrypick:4.3 Considered for cherry-picking into a future 4.3.x release label May 18, 2026
@Repiteo Repiteo removed cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release cherrypick:4.5 Considered for cherry-picking into a future 4.5.x release labels May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The frame duration setting of the first frame of AnimatedSprite2D is ignored in the first loop

7 participants