From d2ca7a1d152321f385181986d442cf2431fcf2e2 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Sat, 7 Sep 2024 13:28:26 -0400 Subject: [PATCH] GH-796 Guard against `get_current_scene` returning null --- .../nodes/dialogue/dialogue_message.cpp | 10 +++++ src/script/nodes/properties/property_get.cpp | 3 +- src/script/nodes/properties/property_set.cpp | 3 +- src/script/nodes/utilities/print_string.cpp | 42 +++++++++++-------- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/script/nodes/dialogue/dialogue_message.cpp b/src/script/nodes/dialogue/dialogue_message.cpp index f612a79c..2a0e9e9b 100644 --- a/src/script/nodes/dialogue/dialogue_message.cpp +++ b/src/script/nodes/dialogue/dialogue_message.cpp @@ -25,6 +25,7 @@ #include #include #include +#include class OScriptNodeDialogueMessageInstance : public OScriptNodeInstance { @@ -105,6 +106,15 @@ class OScriptNodeDialogueMessageInstance : public OScriptNodeInstance state->connect_to_signal(_ui, "show_message_finished", Array()); Node* root = node->get_tree()->get_current_scene(); + if (!root) + root = node->get_tree()->get_root()->get_child(0); + + if (!root) + { + p_context.set_error("Cannot find root node"); + return -1 | STEP_FLAG_END; + } + if (!root->is_node_ready()) root->call_deferred("add_child", _ui); else diff --git a/src/script/nodes/properties/property_get.cpp b/src/script/nodes/properties/property_get.cpp index 5025172b..ff81159f 100644 --- a/src/script/nodes/properties/property_get.cpp +++ b/src/script/nodes/properties/property_get.cpp @@ -21,6 +21,7 @@ #include #include +#include class OScriptNodePropertyGetInstance : public OScriptNodeInstance { @@ -34,7 +35,7 @@ class OScriptNodePropertyGetInstance : public OScriptNodeInstance Node* _get_node_path_target(OScriptExecutionContext& p_context) { if (Node* owner = Object::cast_to(p_context.get_owner())) - return owner->get_tree()->get_current_scene()->get_node_or_null(_node_path); + return owner->get_tree()->get_root()->get_node_or_null(_node_path); return nullptr; } diff --git a/src/script/nodes/properties/property_set.cpp b/src/script/nodes/properties/property_set.cpp index 93ee6d84..0a3145c6 100644 --- a/src/script/nodes/properties/property_set.cpp +++ b/src/script/nodes/properties/property_set.cpp @@ -21,6 +21,7 @@ #include #include +#include class OScriptNodePropertySetInstance : public OScriptNodeInstance { @@ -34,7 +35,7 @@ class OScriptNodePropertySetInstance : public OScriptNodeInstance Node* _get_node_path_target(OScriptExecutionContext& p_context) { if (Node* owner = Object::cast_to(p_context.get_owner())) - return owner->get_tree()->get_current_scene()->get_node_or_null(_node_path); + return owner->get_tree()->get_root()->get_node_or_null(_node_path); return nullptr; } diff --git a/src/script/nodes/utilities/print_string.cpp b/src/script/nodes/utilities/print_string.cpp index 4e1d94b5..9b724f6a 100644 --- a/src/script/nodes/utilities/print_string.cpp +++ b/src/script/nodes/utilities/print_string.cpp @@ -92,25 +92,31 @@ int OScriptNodePrintStringInstance::step(OScriptExecutionContext& p_context) { SceneTree* tree = _get_tree(p_context); - Node* container = _get_or_create_ui_container(tree->get_current_scene()); - if (container) + Node* root = tree->get_current_scene(); + if (!root) + root = tree->get_root()->get_child(0); + + if (root) { - Variant text = p_context.get_input(0); - - RichTextLabel* label = memnew(RichTextLabel); - label->set_fit_content(true); - label->set_use_bbcode(true); - label->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); - label->set_h_size_flags(Control::SIZE_EXPAND_FILL); - label->set_autowrap_mode(TextServer::AUTOWRAP_OFF); - container->add_child(label); - - label->push_color(p_context.get_input(3)); - label->append_text(text); - label->pop(); - - Ref timer = tree->create_timer(p_context.get_input(4)); - timer->connect("timeout", Callable(label, "queue_free")); + if (Node* container = _get_or_create_ui_container(root)) + { + Variant text = p_context.get_input(0); + + RichTextLabel* label = memnew(RichTextLabel); + label->set_fit_content(true); + label->set_use_bbcode(true); + label->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); + label->set_h_size_flags(Control::SIZE_EXPAND_FILL); + label->set_autowrap_mode(TextServer::AUTOWRAP_OFF); + container->add_child(label); + + label->push_color(p_context.get_input(3)); + label->append_text(text); + label->pop(); + + Ref timer = tree->create_timer(p_context.get_input(4)); + timer->connect("timeout", Callable(label, "queue_free")); + } } }