Skip to content

Commit 685c7e9

Browse files
committed
Merge pull request godotengine#100437 from KoBeWi/ruaninstancequestionmark
Add `is_instance()` helper method to Node
2 parents 4b8cd07 + 11adf40 commit 685c7e9

File tree

8 files changed

+29
-26
lines changed

8 files changed

+29
-26
lines changed

editor/docks/scene_tree_dock.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
10271027
return;
10281028
}
10291029

1030-
if (!node->get_scene_file_path().is_empty()) {
1030+
if (node->is_instance()) {
10311031
accept->set_text(TTR("Instantiated scenes can't become root"));
10321032
accept->popup_centered();
10331033
return;
@@ -1111,8 +1111,8 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
11111111
Node *node = remove_list.front()->get();
11121112
if (node == editor_data->get_edited_scene_root()) {
11131113
msg = vformat(TTR("Delete the root node \"%s\"?"), node->get_name());
1114-
} else if (node->get_scene_file_path().is_empty() && node->get_child_count() > 0) {
1115-
// Display this message only for non-instantiated scenes
1114+
} else if (!node->is_instance() && node->get_child_count() > 0) {
1115+
// Display this message only for non-instantiated scenes.
11161116
msg = vformat(TTR("Delete node \"%s\" and its children?"), node->get_name());
11171117
} else {
11181118
msg = vformat(TTR("Delete node \"%s\"?"), node->get_name());
@@ -1169,7 +1169,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
11691169
break;
11701170
}
11711171

1172-
if (tocopy != editor_data->get_edited_scene_root() && !tocopy->get_scene_file_path().is_empty()) {
1172+
if (tocopy != editor_data->get_edited_scene_root() && tocopy->is_instance()) {
11731173
accept->set_text(TTR("Can't save the branch of an already instantiated scene.\nTo create a variation of a scene, you can make an inherited scene based on the instantiated scene using Scene > New Inherited Scene... instead."));
11741174
accept->popup_centered();
11751175
break;
@@ -1281,7 +1281,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
12811281
if (e) {
12821282
Node *node = e->get();
12831283
if (node) {
1284-
bool is_external = (!node->get_scene_file_path().is_empty());
1284+
bool is_external = node->is_instance();
12851285
bool is_top_level = node->get_owner() == nullptr;
12861286
if (!is_external || is_top_level) {
12871287
break;
@@ -1353,7 +1353,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
13531353
break;
13541354
}
13551355

1356-
ERR_FAIL_COND(node->get_scene_file_path().is_empty());
1356+
ERR_FAIL_COND(!node->is_instance());
13571357
undo_redo->create_action(TTR("Make Local"));
13581358
undo_redo->add_do_method(node, "set_scene_file_path", "");
13591359
undo_redo->add_undo_method(node, "set_scene_file_path", node->get_scene_file_path());
@@ -2327,7 +2327,7 @@ bool SceneTreeDock::_validate_no_instance() {
23272327
const List<Node *> &selection = editor_selection->get_top_selected_node_list();
23282328

23292329
for (Node *E : selection) {
2330-
if (E != edited_scene && !E->get_scene_file_path().is_empty()) {
2330+
if (E != edited_scene && E->is_instance()) {
23312331
accept->set_text(TTR("This operation can't be done on instantiated scenes."));
23322332
accept->popup_centered();
23332333
return false;
@@ -3894,7 +3894,7 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
38943894

38953895
bool can_replace = true;
38963896
for (Node *E : selection) {
3897-
if (E != edited_scene && (E->get_owner() != edited_scene || !E->get_scene_file_path().is_empty())) {
3897+
if (E != edited_scene && (E->get_owner() != edited_scene || E->is_instance())) {
38983898
can_replace = false;
38993899
break;
39003900
}
@@ -3957,7 +3957,7 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
39573957
}
39583958

39593959
if (selection.size() == 1) {
3960-
bool is_external = (!selection.front()->get()->get_scene_file_path().is_empty());
3960+
bool is_external = selection.front()->get()->is_instance();
39613961
if (is_external) {
39623962
bool is_inherited = selection.front()->get()->get_scene_inherited_state().is_valid();
39633963
bool is_top_level = selection.front()->get()->get_owner() == nullptr;
@@ -3994,7 +3994,7 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
39943994
}
39953995
menu->add_separator();
39963996

3997-
if (full_selection.size() == 1 && !selection.front()->get()->get_scene_file_path().is_empty()) {
3997+
if (full_selection.size() == 1 && selection.front()->get()->is_instance()) {
39983998
menu->add_icon_shortcut(get_editor_theme_icon(SNAME("ShowInFileSystem")), ED_GET_SHORTCUT("scene_tree/show_in_file_system"), TOOL_SHOW_IN_FILE_SYSTEM);
39993999
}
40004000

editor/editor_data.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, HashSet<Str
668668

669669
if (p_node == p_root) {
670670
ss = p_node->get_scene_inherited_state();
671-
} else if (!p_node->get_scene_file_path().is_empty()) {
671+
} else if (p_node->is_instance()) {
672672
ss = p_node->get_scene_instance_state();
673673
}
674674

@@ -773,7 +773,7 @@ void EditorData::set_edited_scene_root(Node *p_root) {
773773
ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
774774
edited_scene.write[current_edited_scene].root = p_root;
775775
if (p_root) {
776-
if (!p_root->get_scene_file_path().is_empty()) {
776+
if (p_root->is_instance()) {
777777
edited_scene.write[current_edited_scene].path = p_root->get_scene_file_path();
778778
} else {
779779
p_root->set_scene_file_path(edited_scene[current_edited_scene].path);
@@ -839,7 +839,7 @@ Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
839839
Ref<Script> s = edited_scene[p_idx].root->get_script();
840840
if (s.is_null() && edited_scene[p_idx].root->get_child_count()) {
841841
Node *n = edited_scene[p_idx].root->get_child(0);
842-
while (s.is_null() && n && n->get_scene_file_path().is_empty()) {
842+
while (s.is_null() && n && !n->is_instance()) {
843843
s = n->get_script();
844844
n = n->get_parent();
845845
}

editor/scene/canvas_item_editor_plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ bool CanvasItemEditor::_gui_input_open_scene_on_double_click(const Ref<InputEven
16101610
List<CanvasItem *> selection = _get_edited_canvas_items();
16111611
if (selection.size() == 1) {
16121612
CanvasItem *ci = selection.front()->get();
1613-
if (!ci->get_scene_file_path().is_empty() && ci != EditorNode::get_singleton()->get_edited_scene()) {
1613+
if (ci->is_instance() && ci != EditorNode::get_singleton()->get_edited_scene()) {
16141614
EditorNode::get_singleton()->load_scene(ci->get_scene_file_path());
16151615
return true;
16161616
}

editor/scene/scene_tree_editor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ void SceneTreeEditor::_update_node_tooltip(Node *p_node, TreeItem *p_item) {
670670
p_item->add_button(0, get_editor_theme_icon(SNAME("InstanceOptions")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
671671
}
672672
tooltip += String("\n" + TTR("Inherits:") + " " + p_node->get_scene_inherited_state()->get_path());
673-
} else if (p_node != get_scene_node() && !p_node->get_scene_file_path().is_empty() && can_open_instance) {
673+
} else if (p_node != get_scene_node() && p_node->is_instance() && can_open_instance) {
674674
if (p_item->get_button_by_id(0, BUTTON_SUBSCENE) == -1) {
675675
p_item->add_button(0, get_editor_theme_icon(SNAME("InstanceOptions")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
676676
}
@@ -1532,7 +1532,7 @@ void SceneTreeEditor::rename_node(Node *p_node, const String &p_name, TreeItem *
15321532
// Trim leading/trailing whitespace to prevent node names from containing accidental whitespace,
15331533
// which would make it more difficult to get the node via `get_node()`.
15341534
new_name = new_name.strip_edges();
1535-
if (new_name.is_empty() && p_node->get_owner() != nullptr && !p_node->get_scene_file_path().is_empty()) {
1535+
if (new_name.is_empty() && p_node->get_owner() != nullptr && p_node->is_instance()) {
15361536
// If name is empty and node is root of an instance, revert to the original name.
15371537
const Ref<PackedScene> node_scene = ResourceLoader::load(p_node->get_scene_file_path());
15381538
if (node_scene.is_valid()) {
@@ -1966,7 +1966,7 @@ bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_d
19661966
for (int i = 0; i < nodes.size(); i++) {
19671967
Node *n = get_node(nodes[i]);
19681968
// Nodes from an instantiated scene can't be rearranged.
1969-
if (n && n->get_owner() && n->get_owner() != get_scene_node() && !n->get_owner()->get_scene_file_path().is_empty()) {
1969+
if (n && n->get_owner() && n->get_owner() != get_scene_node() && n->get_owner()->is_instance()) {
19701970
return false;
19711971
}
19721972
}

scene/main/node.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,7 +2797,7 @@ Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) c
27972797
nip->set_instance_path(ip->get_instance_path());
27982798
node = nip;
27992799

2800-
} else if ((p_flags & DUPLICATE_USE_INSTANTIATION) && !get_scene_file_path().is_empty()) {
2800+
} else if ((p_flags & DUPLICATE_USE_INSTANTIATION) && is_instance()) {
28012801
Ref<PackedScene> res = ResourceLoader::load(get_scene_file_path());
28022802
ERR_FAIL_COND_V(res.is_null(), nullptr);
28032803
PackedScene::GenEditState edit_state = PackedScene::GEN_EDIT_STATE_DISABLED;
@@ -2822,7 +2822,7 @@ Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) c
28222822
ERR_FAIL_NULL_V(node, nullptr);
28232823
}
28242824

2825-
if (!get_scene_file_path().is_empty()) { //an instance
2825+
if (is_instance()) {
28262826
node->set_scene_file_path(get_scene_file_path());
28272827
node->data.editable_instance = data.editable_instance;
28282828
}
@@ -2853,7 +2853,7 @@ Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) c
28532853

28542854
node_tree.push_back(descendant);
28552855

2856-
if (!descendant->get_scene_file_path().is_empty() && instance_roots.has(descendant->get_owner())) {
2856+
if (descendant->is_instance() && instance_roots.has(descendant->get_owner())) {
28572857
instance_roots.push_back(descendant);
28582858
}
28592859
}

scene/main/node.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,10 @@ class Node : public Object {
821821
void set_thread_safe(const StringName &p_property, const Variant &p_value);
822822
void notify_thread_safe(int p_notification);
823823

824+
/* HELPER */
825+
826+
bool is_instance() const { return !data.scene_file_path.is_empty(); }
827+
824828
// These inherited functions need proper multithread locking when overridden in Node.
825829
#ifdef DEBUG_ENABLED
826830

scene/property_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ Vector<SceneState::PackState> PropertyUtils::get_node_states_stack(const Node *p
258258
}
259259
}
260260
break;
261-
} else if (!n->get_scene_file_path().is_empty()) {
261+
} else if (n->is_instance()) {
262262
const Ref<SceneState> &state = n->get_scene_instance_state();
263263
_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack);
264264
}

scene/resources/packed_scene.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Has
749749

750750
// save the child instantiated scenes that are chosen as editable, so they can be restored
751751
// upon load back
752-
if (p_node != p_owner && !p_node->get_scene_file_path().is_empty() && p_owner->is_editable_instance(p_node)) {
752+
if (p_node != p_owner && p_node->is_instance() && p_owner->is_editable_instance(p_node)) {
753753
editable_instances.push_back(p_owner->get_path_to(p_node));
754754
// Node is the root of an editable instance.
755755
is_editable_instance = true;
@@ -783,7 +783,7 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Has
783783
bool instantiated_by_owner = false;
784784
Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, p_owner, &instantiated_by_owner);
785785

786-
if (!p_node->get_scene_file_path().is_empty() && p_node->get_owner() == p_owner && instantiated_by_owner) {
786+
if (p_node->is_instance() && p_node->get_owner() == p_owner && instantiated_by_owner) {
787787
if (p_node->get_scene_instance_load_placeholder()) {
788788
//it's a placeholder, use the placeholder path
789789
nd.instance = _vm_get_variant(p_node->get_scene_file_path(), variant_map);
@@ -1123,7 +1123,7 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, HashMap<String
11231123

11241124
ERR_CONTINUE(!common_parent);
11251125

1126-
if (common_parent != p_owner && common_parent->get_scene_file_path().is_empty()) {
1126+
if (common_parent != p_owner && !common_parent->is_instance()) {
11271127
common_parent = common_parent->get_owner();
11281128
}
11291129

@@ -1183,8 +1183,7 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, HashMap<String
11831183

11841184
nl = nullptr;
11851185
} else {
1186-
if (!nl->get_scene_file_path().is_empty()) {
1187-
//is an instance
1186+
if (nl->is_instance()) {
11881187
Ref<SceneState> state = nl->get_scene_instance_state();
11891188
if (state.is_valid()) {
11901189
int from_node = state->find_node_by_path(nl->get_path_to(p_node));

0 commit comments

Comments
 (0)