Skip to content

Commit 11adf40

Browse files
committed
Add is_instance() helper method to Node
1 parent 4d1f26e commit 11adf40

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
@@ -1024,7 +1024,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
10241024
return;
10251025
}
10261026

1027-
if (!node->get_scene_file_path().is_empty()) {
1027+
if (node->is_instance()) {
10281028
accept->set_text(TTR("Instantiated scenes can't become root"));
10291029
accept->popup_centered();
10301030
return;
@@ -1108,8 +1108,8 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
11081108
Node *node = remove_list.front()->get();
11091109
if (node == editor_data->get_edited_scene_root()) {
11101110
msg = vformat(TTR("Delete the root node \"%s\"?"), node->get_name());
1111-
} else if (node->get_scene_file_path().is_empty() && node->get_child_count() > 0) {
1112-
// Display this message only for non-instantiated scenes
1111+
} else if (!node->is_instance() && node->get_child_count() > 0) {
1112+
// Display this message only for non-instantiated scenes.
11131113
msg = vformat(TTR("Delete node \"%s\" and its children?"), node->get_name());
11141114
} else {
11151115
msg = vformat(TTR("Delete node \"%s\"?"), node->get_name());
@@ -1166,7 +1166,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
11661166
break;
11671167
}
11681168

1169-
if (tocopy != editor_data->get_edited_scene_root() && !tocopy->get_scene_file_path().is_empty()) {
1169+
if (tocopy != editor_data->get_edited_scene_root() && tocopy->is_instance()) {
11701170
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."));
11711171
accept->popup_centered();
11721172
break;
@@ -1278,7 +1278,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
12781278
if (e) {
12791279
Node *node = e->get();
12801280
if (node) {
1281-
bool is_external = (!node->get_scene_file_path().is_empty());
1281+
bool is_external = node->is_instance();
12821282
bool is_top_level = node->get_owner() == nullptr;
12831283
if (!is_external || is_top_level) {
12841284
break;
@@ -1350,7 +1350,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
13501350
break;
13511351
}
13521352

1353-
ERR_FAIL_COND(node->get_scene_file_path().is_empty());
1353+
ERR_FAIL_COND(!node->is_instance());
13541354
undo_redo->create_action(TTR("Make Local"));
13551355
undo_redo->add_do_method(node, "set_scene_file_path", "");
13561356
undo_redo->add_undo_method(node, "set_scene_file_path", node->get_scene_file_path());
@@ -2319,7 +2319,7 @@ bool SceneTreeDock::_validate_no_instance() {
23192319
List<Node *> selection = editor_selection->get_top_selected_node_list();
23202320

23212321
for (Node *E : selection) {
2322-
if (E != edited_scene && !E->get_scene_file_path().is_empty()) {
2322+
if (E != edited_scene && E->is_instance()) {
23232323
accept->set_text(TTR("This operation can't be done on instantiated scenes."));
23242324
accept->popup_centered();
23252325
return false;
@@ -3856,7 +3856,7 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
38563856

38573857
bool can_replace = true;
38583858
for (Node *E : selection) {
3859-
if (E != edited_scene && (E->get_owner() != edited_scene || !E->get_scene_file_path().is_empty())) {
3859+
if (E != edited_scene && (E->get_owner() != edited_scene || E->is_instance())) {
38603860
can_replace = false;
38613861
break;
38623862
}
@@ -3919,7 +3919,7 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
39193919
}
39203920

39213921
if (selection.size() == 1) {
3922-
bool is_external = (!selection.front()->get()->get_scene_file_path().is_empty());
3922+
bool is_external = selection.front()->get()->is_instance();
39233923
if (is_external) {
39243924
bool is_inherited = selection.front()->get()->get_scene_inherited_state().is_valid();
39253925
bool is_top_level = selection.front()->get()->get_owner() == nullptr;
@@ -3956,7 +3956,7 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
39563956
}
39573957
menu->add_separator();
39583958

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

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
@@ -1607,7 +1607,7 @@ bool CanvasItemEditor::_gui_input_open_scene_on_double_click(const Ref<InputEven
16071607
List<CanvasItem *> selection = _get_edited_canvas_items();
16081608
if (selection.size() == 1) {
16091609
CanvasItem *ci = selection.front()->get();
1610-
if (!ci->get_scene_file_path().is_empty() && ci != EditorNode::get_singleton()->get_edited_scene()) {
1610+
if (ci->is_instance() && ci != EditorNode::get_singleton()->get_edited_scene()) {
16111611
EditorNode::get_singleton()->load_scene(ci->get_scene_file_path());
16121612
return true;
16131613
}

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
}
@@ -1521,7 +1521,7 @@ void SceneTreeEditor::rename_node(Node *p_node, const String &p_name, TreeItem *
15211521
// Trim leading/trailing whitespace to prevent node names from containing accidental whitespace,
15221522
// which would make it more difficult to get the node via `get_node()`.
15231523
new_name = new_name.strip_edges();
1524-
if (new_name.is_empty() && p_node->get_owner() != nullptr && !p_node->get_scene_file_path().is_empty()) {
1524+
if (new_name.is_empty() && p_node->get_owner() != nullptr && p_node->is_instance()) {
15251525
// If name is empty and node is root of an instance, revert to the original name.
15261526
const Ref<PackedScene> node_scene = ResourceLoader::load(p_node->get_scene_file_path());
15271527
if (node_scene.is_valid()) {
@@ -1955,7 +1955,7 @@ bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_d
19551955
for (int i = 0; i < nodes.size(); i++) {
19561956
Node *n = get_node(nodes[i]);
19571957
// Nodes from an instantiated scene can't be rearranged.
1958-
if (n && n->get_owner() && n->get_owner() != get_scene_node() && !n->get_owner()->get_scene_file_path().is_empty()) {
1958+
if (n && n->get_owner() && n->get_owner() != get_scene_node() && n->get_owner()->is_instance()) {
19591959
return false;
19601960
}
19611961
}

scene/main/node.cpp

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

2775-
} else if ((p_flags & DUPLICATE_USE_INSTANTIATION) && !get_scene_file_path().is_empty()) {
2775+
} else if ((p_flags & DUPLICATE_USE_INSTANTIATION) && is_instance()) {
27762776
Ref<PackedScene> res = ResourceLoader::load(get_scene_file_path());
27772777
ERR_FAIL_COND_V(res.is_null(), nullptr);
27782778
PackedScene::GenEditState edit_state = PackedScene::GEN_EDIT_STATE_DISABLED;
@@ -2797,7 +2797,7 @@ Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) c
27972797
ERR_FAIL_NULL_V(node, nullptr);
27982798
}
27992799

2800-
if (!get_scene_file_path().is_empty()) { //an instance
2800+
if (is_instance()) {
28012801
node->set_scene_file_path(get_scene_file_path());
28022802
node->data.editable_instance = data.editable_instance;
28032803
}
@@ -2828,7 +2828,7 @@ Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) c
28282828

28292829
node_tree.push_back(descendant);
28302830

2831-
if (!descendant->get_scene_file_path().is_empty() && instance_roots.has(descendant->get_owner())) {
2831+
if (descendant->is_instance() && instance_roots.has(descendant->get_owner())) {
28322832
instance_roots.push_back(descendant);
28332833
}
28342834
}

scene/main/node.h

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

821+
/* HELPER */
822+
823+
bool is_instance() const { return !data.scene_file_path.is_empty(); }
824+
821825
// These inherited functions need proper multithread locking when overridden in Node.
822826
#ifdef DEBUG_ENABLED
823827

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
@@ -732,7 +732,7 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Has
732732

733733
// save the child instantiated scenes that are chosen as editable, so they can be restored
734734
// upon load back
735-
if (p_node != p_owner && !p_node->get_scene_file_path().is_empty() && p_owner->is_editable_instance(p_node)) {
735+
if (p_node != p_owner && p_node->is_instance() && p_owner->is_editable_instance(p_node)) {
736736
editable_instances.push_back(p_owner->get_path_to(p_node));
737737
// Node is the root of an editable instance.
738738
is_editable_instance = true;
@@ -766,7 +766,7 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Has
766766
bool instantiated_by_owner = false;
767767
Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, p_owner, &instantiated_by_owner);
768768

769-
if (!p_node->get_scene_file_path().is_empty() && p_node->get_owner() == p_owner && instantiated_by_owner) {
769+
if (p_node->is_instance() && p_node->get_owner() == p_owner && instantiated_by_owner) {
770770
if (p_node->get_scene_instance_load_placeholder()) {
771771
//it's a placeholder, use the placeholder path
772772
nd.instance = _vm_get_variant(p_node->get_scene_file_path(), variant_map);
@@ -1106,7 +1106,7 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, HashMap<String
11061106

11071107
ERR_CONTINUE(!common_parent);
11081108

1109-
if (common_parent != p_owner && common_parent->get_scene_file_path().is_empty()) {
1109+
if (common_parent != p_owner && !common_parent->is_instance()) {
11101110
common_parent = common_parent->get_owner();
11111111
}
11121112

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

11671167
nl = nullptr;
11681168
} else {
1169-
if (!nl->get_scene_file_path().is_empty()) {
1170-
//is an instance
1169+
if (nl->is_instance()) {
11711170
Ref<SceneState> state = nl->get_scene_instance_state();
11721171
if (state.is_valid()) {
11731172
int from_node = state->find_node_by_path(nl->get_path_to(p_node));

0 commit comments

Comments
 (0)