diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp index 542011618d..3018fd3ae6 100644 --- a/scene/animation/tween.cpp +++ b/scene/animation/tween.cpp @@ -36,6 +36,10 @@ void Tweener::set_tween(Ref p_tween) { tween = p_tween; } +void Tweener::clear_tween() { + tween.unref(); +} + void Tweener::_bind_methods() { ADD_SIGNAL(MethodInfo("finished")); } @@ -53,7 +57,7 @@ void Tween::start_tweeners() { Ref Tween::tween_property(Object *p_target, NodePath p_property, Variant p_to, float p_duration) { ERR_FAIL_NULL_V(p_target, nullptr); - ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); + ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree."); ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); Ref tweener = memnew(PropertyTweener(p_target, p_property, p_to, p_duration)); @@ -62,7 +66,7 @@ Ref Tween::tween_property(Object *p_target, NodePath p_property } Ref Tween::tween_interval(float p_time) { - ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); + ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree."); ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); Ref tweener = memnew(IntervalTweener(p_time)); @@ -71,7 +75,7 @@ Ref Tween::tween_interval(float p_time) { } Ref Tween::tween_callback(Callable p_callback) { - ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); + ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree."); ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); Ref tweener = memnew(CallbackTweener(p_callback)); @@ -80,7 +84,7 @@ Ref Tween::tween_callback(Callable p_callback) { } Ref Tween::tween_method(Callable p_callback, float p_from, float p_to, float p_duration) { - ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); + ERR_FAIL_COND_V_MSG(!valid, nullptr, "Tween invalid. Either finished or created outside scene tree."); ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); Ref tweener = memnew(MethodTweener(p_callback, p_from, p_to, p_duration)); @@ -88,9 +92,7 @@ Ref Tween::tween_method(Callable p_callback, float p_from, float return tweener; } -Ref Tween::append(Ref p_tweener) { - ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); - ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); +void Tween::append(Ref p_tweener) { p_tweener->set_tween(this); if (parallel_enabled) { @@ -102,8 +104,6 @@ Ref Tween::append(Ref p_tweener) { tweeners.resize(current_step + 1); tweeners.write[current_step].push_back(p_tweener); - - return this; } void Tween::stop() { @@ -117,7 +117,7 @@ void Tween::pause() { } void Tween::play() { - ERR_FAIL_COND_MSG(invalid, "Tween invalid, can't play."); + ERR_FAIL_COND_MSG(!valid, "Tween invalid. Either finished or created outside scene tree."); ERR_FAIL_COND_MSG(dead, "Can't play finished Tween, use stop() first to reset its state."); running = true; } @@ -132,11 +132,22 @@ bool Tween::is_running() { } void Tween::set_valid(bool p_valid) { - invalid = !p_valid; + valid = p_valid; } bool Tween::is_valid() { - return invalid; + return valid; +} + +void Tween::clear() { + valid = false; + + for (List> &step : tweeners) { + for (Ref &tweener : step) { + tweener->clear_tween(); + } + } + tweeners.clear(); } Ref Tween::bind_node(Node *p_node) { diff --git a/scene/animation/tween.h b/scene/animation/tween.h index 947cdb7c2d..953d573539 100644 --- a/scene/animation/tween.h +++ b/scene/animation/tween.h @@ -43,6 +43,7 @@ public: virtual void set_tween(Ref p_tween); virtual void start() = 0; virtual bool step(float &r_delta) = 0; + void clear_tween(); protected: static void _bind_methods(); @@ -111,7 +112,7 @@ private: bool started = false; bool running = true; bool dead = false; - bool invalid = true; + bool valid = false; bool default_parallel = false; bool parallel_enabled = false; @@ -128,7 +129,7 @@ public: Ref tween_interval(float p_time); Ref tween_callback(Callable p_callback); Ref tween_method(Callable p_callback, float p_from, float p_to, float p_duration); - Ref append(Ref p_tweener); + void append(Ref p_tweener); bool custom_step(float p_delta); void stop(); @@ -139,6 +140,7 @@ public: bool is_running(); void set_valid(bool p_valid); bool is_valid(); + void clear(); Ref bind_node(Node *p_node); Ref set_process_mode(TweenProcessMode p_mode); diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp index 8452f1cb1a..e1b1b356a9 100644 --- a/scene/main/scene_tree.cpp +++ b/scene/main/scene_tree.cpp @@ -544,7 +544,7 @@ void SceneTree::process_tweens(float p_delta, bool p_physics) { } if (!E->get()->step(p_delta)) { - E->get()->set_valid(false); + E->get()->clear(); tweens.erase(E); } if (E == L) {