Skip to content

Commit

Permalink
Merge pull request #88856 from AThousandShips/tween_fix
Browse files Browse the repository at this point in the history
[Animation] Improvements to `Tween` memory management
  • Loading branch information
akien-mga committed Feb 27, 2024
2 parents 6d8ca17 + 92814ba commit 592d2cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
29 changes: 14 additions & 15 deletions scene/animation/tween.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ Tween::interpolater Tween::interpolaters[Tween::TRANS_MAX][Tween::EASE_MAX] = {
};

void Tweener::set_tween(const Ref<Tween> &p_tween) {
tween = p_tween;
tween_id = p_tween->get_instance_id();
}

void Tweener::clear_tween() {
tween.unref();
Ref<Tween> Tweener::_get_tween() {
return Ref<Tween>(ObjectDB::get_instance(tween_id));
}

void Tweener::_bind_methods() {
Expand Down Expand Up @@ -192,12 +192,6 @@ bool Tween::is_valid() {

void Tween::clear() {
valid = false;

for (List<Ref<Tweener>> &step : tweeners) {
for (Ref<Tweener> &tweener : step) {
tweener->clear_tween();
}
}
tweeners.clear();
}

Expand Down Expand Up @@ -504,6 +498,7 @@ Tween::Tween(bool p_valid) {
}

Ref<PropertyTweener> PropertyTweener::from(const Variant &p_value) {
Ref<Tween> tween = _get_tween();
ERR_FAIL_COND_V(tween.is_null(), nullptr);

Variant from_value = p_value;
Expand Down Expand Up @@ -592,6 +587,8 @@ bool PropertyTweener::step(double &r_delta) {
do_continue_delayed = false;
}

Ref<Tween> tween = _get_tween();

double time = MIN(elapsed_time - delay, duration);
if (time < duration) {
if (custom_method.is_valid()) {
Expand Down Expand Up @@ -623,12 +620,12 @@ bool PropertyTweener::step(double &r_delta) {
}

void PropertyTweener::set_tween(const Ref<Tween> &p_tween) {
tween = p_tween;
Tweener::set_tween(p_tween);
if (trans_type == Tween::TRANS_MAX) {
trans_type = tween->get_trans();
trans_type = p_tween->get_trans();
}
if (ease_type == Tween::EASE_MAX) {
ease_type = tween->get_ease();
ease_type = p_tween->get_ease();
}
}

Expand Down Expand Up @@ -781,6 +778,8 @@ bool MethodTweener::step(double &r_delta) {
return true;
}

Ref<Tween> tween = _get_tween();

Variant current_val;
double time = MIN(elapsed_time - delay, duration);
if (time < duration) {
Expand Down Expand Up @@ -810,12 +809,12 @@ bool MethodTweener::step(double &r_delta) {
}

void MethodTweener::set_tween(const Ref<Tween> &p_tween) {
tween = p_tween;
Tweener::set_tween(p_tween);
if (trans_type == Tween::TRANS_MAX) {
trans_type = tween->get_trans();
trans_type = p_tween->get_trans();
}
if (ease_type == Tween::EASE_MAX) {
ease_type = tween->get_ease();
ease_type = p_tween->get_ease();
}
}

Expand Down
7 changes: 4 additions & 3 deletions scene/animation/tween.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ class Node;
class Tweener : public RefCounted {
GDCLASS(Tweener, RefCounted);

ObjectID tween_id;

public:
virtual void set_tween(const Ref<Tween> &p_tween);
virtual void start() = 0;
virtual bool step(double &r_delta) = 0;
void clear_tween();

protected:
static void _bind_methods();

Ref<Tween> tween;
Ref<Tween> _get_tween();

double elapsed_time = 0;
bool finished = false;
};
Expand Down Expand Up @@ -291,7 +293,6 @@ class MethodTweener : public Tweener {
Tween::TransitionType trans_type = Tween::TRANS_MAX;
Tween::EaseType ease_type = Tween::EASE_MAX;

Ref<Tween> tween;
Variant initial_val;
Variant delta_val;
Variant final_val;
Expand Down

0 comments on commit 592d2cb

Please sign in to comment.