New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make AnimatedSprite2D update animation property when SpriteFrames changes #58966
Conversation
2f17966
to
5591575
Compare
5591575
to
b1bbe52
Compare
Changed the files according to the suggestions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@L4Vo5 Changes made in AnimatedSprite2D
would need to be done in AnimatedSprite3D
too.
Also as you can see static checks fail (formatting issues), you'd need to apply clang-format to fix these. I recommend adding relevant pre-commit hooks to run it automatically when you commit (so you can fix formatting issues immediately).
scene/resources/sprite_frames.cpp
Outdated
animations.erase(p_anim); | ||
emit_changed(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HashMap::erase
returns a bool telling whether the erasing happened.
animations.erase(p_anim); | |
emit_changed(); | |
if (animations.erase(p_anim)) { | |
emit_changed(); | |
} |
@@ -261,6 +261,10 @@ void AnimatedSprite2D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) { | |||
frames = p_frames; | |||
if (frames.is_valid()) { | |||
frames->connect("changed", callable_mp(this, &AnimatedSprite2D::_res_changed)); | |||
Vector<String> animation_names = frames->get_animation_names(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akien-mga This returns sorted animation names, as desired (so its first element is indeed what we want):
godot/scene/resources/sprite_frames.cpp
Lines 105 to 112 in 9904a9d
Vector<String> SpriteFrames::get_animation_names() const { | |
Vector<String> names; | |
for (const KeyValue<StringName, Anim> &E : animations) { | |
names.push_back(E.key); | |
} | |
names.sort(); | |
return names; | |
} |
But in other places in the source code SpriteFrames::get_animation_list()
is being used instead and the obtained list is being sorted separately:
godot/scene/resources/sprite_frames.cpp
Lines 99 to 103 in 9904a9d
void SpriteFrames::get_animation_list(List<StringName> *r_animations) const { | |
for (const KeyValue<StringName, Anim> &E : animations) { | |
r_animations->push_back(E.key); | |
} | |
} |
godot/editor/plugins/sprite_frames_editor_plugin.cpp
Lines 985 to 987 in 9904a9d
List<StringName> anim_names; | |
frames->get_animation_list(&anim_names); | |
anim_names.sort_custom<StringName::AlphCompare>(); |
Both approaches seem to result in the same ordering (both end up comparing with is_str_less
). But I think we should be consistent and use one over the other. I'd guess sorting a list is probably slower (it probably doesn't matter though) but in the list version there's no StringName -> String conversion (which is also cheap I guess). So is any of these approaches preferred?
b1bbe52
to
2adf55c
Compare
Thanks for the clang help!
Should that be a separate PR? |
Makes AnimatedSprite2D and AnimatedSprite3D have a valid animation when setting or changing the SpriteFrames. Removes error on set_animation when SpriteFrames is null, which caused errors even with its own default value.
2adf55c
to
74a905b
Compare
Applied changes to AnimatedSprite3D following #64155, which brought the same errors to it (but also made it infinitely easier to make the same changes) |
AnimatedSprite underwent many changes recently and both issues are now closed. Is this PR still relevant? |
Hmm, the current fix for 28658 is only in the inspector: it does nothing when deleting animations in code. Still, that makes the issue even more minor than it was before, so this PR is probably not worth keeping as it'd require a full revision to update it just for that. |
AnimatedSprite2D will ensure it has a valid animation when the SpriteFrames is set, and on its "changed" signal.
To that end, makes SpriteFrames emit the "changed" signal when one or all of its animations are deleted.
Note that the animation property will remain as-is if the SpriteFrames has no animations at all.
Removes error message on set_animation when SpriteFrames is null, which caused "no animation with name X" errors even with the "default" value of freshly created instances.
So, this allows the animation property to be set even without a valid SpriteFrames. And once one is added, the animation will be changed if necessary to make it valid, making the error pointless.
Fixes #45459 and #28658.
Related to #49495 on the 3.x branch.