Browse Source

Merge pull request #70795 from KoBeWi/TweenCounted

Improve `RefCounted` support in `Tween`
Rémi Verschelde 2 years ago
parent
commit
d322c154a6
2 changed files with 20 additions and 0 deletions
  1. 14 0
      scene/animation/tween.cpp
  2. 6 0
      scene/animation/tween.h

+ 14 - 0
scene/animation/tween.cpp

@@ -563,6 +563,10 @@ PropertyTweener::PropertyTweener(Object *p_target, NodePath p_property, Variant
 	base_final_val = p_to;
 	final_val = base_final_val;
 	duration = p_duration;
+
+	if (p_target->is_ref_counted()) {
+		ref_copy = p_target;
+	}
 }
 
 PropertyTweener::PropertyTweener() {
@@ -640,6 +644,11 @@ void CallbackTweener::_bind_methods() {
 
 CallbackTweener::CallbackTweener(Callable p_callback) {
 	callback = p_callback;
+
+	Object *callback_instance = p_callback.get_object();
+	if (callback_instance && callback_instance->is_ref_counted()) {
+		ref_copy = callback_instance;
+	}
 }
 
 CallbackTweener::CallbackTweener() {
@@ -728,6 +737,11 @@ MethodTweener::MethodTweener(Callable p_callback, Variant p_from, Variant p_to,
 	delta_val = Animation::subtract_variant(p_to, p_from);
 	final_val = p_to;
 	duration = p_duration;
+
+	Object *callback_instance = p_callback.get_object();
+	if (callback_instance && callback_instance->is_ref_counted()) {
+		ref_copy = callback_instance;
+	}
 }
 
 MethodTweener::MethodTweener() {

+ 6 - 0
scene/animation/tween.h

@@ -208,6 +208,8 @@ private:
 	Variant final_val;
 	Variant delta_val;
 
+	Ref<RefCounted> ref_copy; // Makes sure that RefCounted objects are not freed too early.
+
 	double duration = 0;
 	Tween::TransitionType trans_type = Tween::TRANS_MAX; // This is set inside set_tween();
 	Tween::EaseType ease_type = Tween::EASE_MAX;
@@ -249,6 +251,8 @@ protected:
 private:
 	Callable callback;
 	double delay = 0;
+
+	Ref<RefCounted> ref_copy;
 };
 
 class MethodTweener : public Tweener {
@@ -280,6 +284,8 @@ private:
 	Variant delta_val;
 	Variant final_val;
 	Callable callback;
+
+	Ref<RefCounted> ref_copy;
 };
 
 #endif // TWEEN_H