summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkobewi <kobewi4e@gmail.com>2024-01-29 14:10:07 +0100
committerkobewi <kobewi4e@gmail.com>2024-01-29 20:18:43 +0100
commit0de8a736dad9bf9ed5b8075946bf1e96a47b20f6 (patch)
treeaa2171a0feb7fa016d970794b82a9c2051a373eb
parentfa48a51183567934984b381ad8ec281cb24d66ba (diff)
downloadredot-engine-0de8a736dad9bf9ed5b8075946bf1e96a47b20f6.tar.gz
Allow Node.create_tween() outside SceneTree
-rw-r--r--doc/classes/Node.xml5
-rw-r--r--scene/main/node.cpp10
2 files changed, 11 insertions, 4 deletions
diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml
index 5dd3d22062..98f75f4d92 100644
--- a/doc/classes/Node.xml
+++ b/doc/classes/Node.xml
@@ -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:
[codeblocks]
[gdscript]
@@ -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">
diff --git a/scene/main/node.cpp b/scene/main/node.cpp
index f25e178d23..b78dfb2f2c 100644
--- a/scene/main/node.cpp
+++ b/scene/main/node.cpp
@@ -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;
}