Skip to content

Commit

Permalink
Avoid infinite recursion when AnimationPlayer.current_animation is se…
Browse files Browse the repository at this point in the history
…t to an empty string
  • Loading branch information
matheusmdx committed Nov 1, 2024
1 parent ef8d981 commit dd9d0b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 17 additions & 10 deletions scene/animation/animation_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name;
if (name.begins_with("playback/play")) { // For backward compatibility.
set_current_animation(p_value);
_set_current_animation(p_value);
} else if (name.begins_with("next/")) {
String which = name.get_slicec('/', 1);
animation_set_next(which, p_value);
Expand Down Expand Up @@ -582,16 +582,10 @@ bool AnimationPlayer::is_playing() const {
}

void AnimationPlayer::set_current_animation(const String &p_animation) {
if (p_animation == "[stop]" || p_animation.is_empty()) {
stop();
} else if (!is_playing()) {
play(p_animation);
} else if (playback.assigned != p_animation) {
float speed = playback.current.speed_scale;
play(p_animation, -1.0, speed, signbit(speed));
} else {
// Same animation, do not replay from start.
if (p_animation.is_empty()) {
return;
}
_set_current_animation(p_animation);
}

String AnimationPlayer::get_current_animation() const {
Expand Down Expand Up @@ -942,6 +936,19 @@ void AnimationPlayer::_rename_animation(const StringName &p_from_name, const Str
}
}

void AnimationPlayer::_set_current_animation(const String &p_animation) {
if (p_animation == "[stop]" || p_animation.is_empty()) {
stop();
} else if (!is_playing()) {
play(p_animation);
} else if (playback.assigned != p_animation) {
float speed = playback.current.speed_scale;
play(p_animation, -1.0, speed, signbit(speed));
} else {
// Same animation, do not replay from start.
}
}

void AnimationPlayer::_bind_methods() {
ClassDB::bind_method(D_METHOD("animation_set_next", "animation_from", "animation_to"), &AnimationPlayer::animation_set_next);
ClassDB::bind_method(D_METHOD("animation_get_next", "animation_from"), &AnimationPlayer::animation_get_next);
Expand Down
2 changes: 2 additions & 0 deletions scene/animation/animation_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class AnimationPlayer : public AnimationMixer {
void _stop_internal(bool p_reset, bool p_keep_state);
void _check_immediately_after_start();

void _set_current_animation(const String &p_animation);

float get_current_blend_amount();

bool playing = false;
Expand Down

0 comments on commit dd9d0b0

Please sign in to comment.