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

Allow Node.create_tween() outside SceneTree #87701

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/classes/Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
<method name="create_tween">
<return type="Tween" />
<description>
Creates a new [Tween] and binds it to this node. Fails if the node is not inside the tree.
Creates a new [Tween] and binds it to this node.
This is the equivalent of doing:
Copy link
Member

Choose a reason for hiding this comment

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

It should be obvious, but might want to clarify here

Suggested change
This is the equivalent of doing:
This is the equivalent of doing (if inside the tree):

Or similar

Copy link
Member Author

@KoBeWi KoBeWi Jan 29, 2024

Choose a reason for hiding this comment

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

Not sure about that. If I add this, it would be worth to mention that the method also works outside the tree, adding overly verbose details. Previously the description just stated the method does not work outside scene tree, now it always works (unless you use custom MainLoop), so I just removed the note. No people complained about that limitation, so if someone assumes from the example that it's impossible to call the method outside the tree, it's not really a problem.

[codeblocks]
[gdscript]
Expand All @@ -215,7 +215,8 @@
GetTree().CreateTween().BindNode(this);
[/csharp]
[/codeblocks]
The Tween will start automatically on the next process frame or physics frame (depending on [enum Tween.TweenProcessMode]).
The Tween will start automatically on the next process frame or physics frame (depending on [enum Tween.TweenProcessMode]). See [method Tween.bind_node] for more info on Tweens bound to nodes.
[b]Note:[/b] The method can still be used when the node is not inside [SceneTree]. It can fail in an unlikely case of using a custom [MainLoop].
</description>
</method>
<method name="duplicate" qualifiers="const">
Expand Down
10 changes: 8 additions & 2 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2326,8 +2326,14 @@ void Node::_propagate_replace_owner(Node *p_owner, Node *p_by_owner) {

Ref<Tween> Node::create_tween() {
ERR_THREAD_GUARD_V(Ref<Tween>());
ERR_FAIL_NULL_V_MSG(data.tree, nullptr, "Can't create Tween when not inside scene tree.");
Ref<Tween> tween = get_tree()->create_tween();

SceneTree *tree = data.tree;
if (!tree) {
tree = SceneTree::get_singleton();
}
ERR_FAIL_NULL_V_MSG(tree, Ref<Tween>(), "No available SceneTree to create the Tween.");

Ref<Tween> tween = tree->create_tween();
tween->bind_node(this);
return tween;
}
Expand Down
Loading