Skip to content
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

Discourage reusing Tweens #74258

Merged
merged 1 commit into from
Mar 5, 2023

Conversation

KoBeWi
Copy link
Member

@KoBeWi KoBeWi commented Mar 2, 2023

Closes #74245
Hopefully this will discourage people from even thinking about this 🙄

@KoBeWi KoBeWi added enhancement documentation cherrypick:3.x Considered for cherry-picking into a future 3.x release topic:animation cherrypick:4.0 labels Mar 2, 2023
@KoBeWi KoBeWi added this to the 4.1 milestone Mar 2, 2023
@KoBeWi KoBeWi requested a review from a team as a code owner March 2, 2023 22:50
doc/classes/Tween.xml Outdated Show resolved Hide resolved
doc/classes/Tween.xml Outdated Show resolved Hide resolved
@follower
Copy link
Contributor

follower commented Mar 3, 2023

A pointer for anyone else looking for more context re: Tween/SceneTreeTween + pause()/stop() code/behaviour, see:

Update: Additionally, the related code has diverged between v4.0 & v3.x backport:

void Tween::stop() {
_stop_internal(true);
}
void Tween::pause() {
_stop_internal(false);
}
void Tween::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;
}
void Tween::kill() {
running = false; // For the sake of is_running().
dead = true;
}
bool Tween::is_running() {
return running;
}
bool Tween::is_valid() {
return valid;
}
void Tween::clear() {
valid = false;
for (List<Ref<Tweener>> &step : tweeners) {
for (Ref<Tweener> &tweener : step) {
tweener->clear_tween();
}
}
tweeners.clear();
}

void SceneTreeTween::stop() {
started = false;
running = false;
dead = false;
total_time = 0;
}
void SceneTreeTween::pause() {
running = false;
}
void SceneTreeTween::play() {
ERR_FAIL_COND_MSG(!valid, "SceneTreeTween invalid. Either finished or created outside scene tree.");
ERR_FAIL_COND_MSG(dead, "Can't play finished SceneTreeTween, use stop() first to reset its state.");
running = true;
}
void SceneTreeTween::kill() {
running = false; // For the sake of is_running().
dead = true;
}
bool SceneTreeTween::is_running() const {
return running;
}
bool SceneTreeTween::is_valid() const {
return valid;
}
void SceneTreeTween::clear() {
valid = false;
for (int i = 0; i < tweeners.size(); i++) {
List<Ref<Tweener>> &step = tweeners.write[i];
for (int j = 0; j < step.size(); j++) {
Ref<Tweener> &tweener = step[j];
tweener->clear_tween();
}
}
tweeners.clear();
}

@KoBeWi KoBeWi force-pushed the do_not_fricking_reuse_tweens branch 2 times, most recently from eda6a14 to 1944a6e Compare March 3, 2023 23:07
@KoBeWi
Copy link
Member Author

KoBeWi commented Mar 3, 2023

Ok I reworked the PR. Instead of saying "this is somewhat supported, but don't do it", I removed all documentation notes that imply reusing (pretending it's not supported at all) and only left a comment that it results in undefined behavior.

@KoBeWi KoBeWi changed the title Add a strong note about not reusing Tweens Discourage reusing Tweens Mar 3, 2023
Copy link
Contributor

@bend-n bend-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just saw some english problems

doc/classes/Tween.xml Outdated Show resolved Hide resolved
[b]Note:[/b] All [Tween]s will automatically start by default. To prevent a [Tween] from autostarting, you can call [method stop] immediately after it is created.
[b]Note:[/b] [Tween]s are processing after all of nodes in the current frame, i.e. after [method Node._process] or [method Node._physics_process] (depending on [enum TweenProcessMode]).
[b]Note:[/b] Tweens are not designed to be re-used and trying to do so results in an undefined behavior. Create a new Tween for each animation and every time you replay an animation from start. Also Tweens start immediately, so only create a Tween when you want to start animating.
[b]Note:[/b] Tweens are processing after all of nodes in the current frame, i.e. after [method Node._process] or [method Node._physics_process] (depending on [enum TweenProcessMode]).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[b]Note:[/b] Tweens are processing after all of nodes in the current frame, i.e. after [method Node._process] or [method Node._physics_process] (depending on [enum TweenProcessMode]).
[b]Note:[/b] Tweens are processed after all of the nodes in the current frame, i.e. after [method Node._process] or [method Node._physics_process] (depending on [enum TweenProcessMode]).

doc/classes/SceneTree.xml Outdated Show resolved Hide resolved
doc/classes/Node.xml Outdated Show resolved Hide resolved
@KoBeWi KoBeWi force-pushed the do_not_fricking_reuse_tweens branch from 1944a6e to 33c57f4 Compare March 4, 2023 01:26
doc/classes/Tween.xml Outdated Show resolved Hide resolved
Copy link
Contributor

@YuriSizov YuriSizov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this version seems good to me.

@KoBeWi KoBeWi force-pushed the do_not_fricking_reuse_tweens branch from 33c57f4 to 9785b23 Compare March 4, 2023 18:51
@akien-mga akien-mga merged commit 24d4719 into godotengine:master Mar 5, 2023
@akien-mga
Copy link
Member

Thanks!

@KoBeWi KoBeWi deleted the do_not_fricking_reuse_tweens branch March 5, 2023 13:56
@naturally-intelligent
Copy link

What are best practices for persistent Tweens?

https://godotengine.org/qa/148332/best-practices-for-creating-a-persistent-tween-in-godot-4

I'm kinda stuck because if we are supposed to use AnimationPlayer for this, not sure how to dynamically add Animations at runtime either:

https://godotengine.org/qa/148336/how-can-i-add-animations-to-animationplayer-by-gdscript

@KoBeWi
Copy link
Member Author

KoBeWi commented Mar 5, 2023

The answer is in the docs, although probably not obvious for this particular question:

var tween
func animate():
	if tween:
		tween.kill() # Abort the previous animation.
		tween = create_tween()

Create the Tween inside play_effect() instead of _ready(). Still store it in a member variable, but use kill() on the old Tween (note that you don't need to check if it's still running).

@YuriSizov
Copy link
Contributor

Cherry-picked for 4.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherrypick:3.x Considered for cherry-picking into a future 3.x release documentation enhancement topic:animation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lack of documentation on impossibility to reuse SceneTreeTween
9 participants