Skip to content

Commit 3008a05

Browse files
committed
Util: Make node_is_part_of_edited_scene safer
When switching to a different scene in the editor, the previous edited scene root will no longer have a parent and you'll see a pile of "Cannot call method 'is_ancestor_of' on a null value." errors from Godot. Unroll the one liner to add the parent null check and use some intermediate variables to remove duplicate method calls. Note that the is_inside_tree() call is dropped since it's redundant to checking if get_tree() returns null.
1 parent 6f7bb2d commit 3008a05

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

addons/block_code/ui/util.gd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@ extends Object
33

44
## Polyfill of Node.is_part_of_edited_scene(), available to GDScript in Godot 4.3+.
55
static func node_is_part_of_edited_scene(node: Node) -> bool:
6-
return Engine.is_editor_hint() && node.is_inside_tree() && node.get_tree().edited_scene_root && node.get_tree().edited_scene_root.get_parent().is_ancestor_of(node)
6+
if not Engine.is_editor_hint():
7+
return false
8+
9+
var tree := node.get_tree()
10+
if not tree or not tree.edited_scene_root:
11+
return false
12+
13+
var edited_scene_parent := tree.edited_scene_root.get_parent()
14+
return edited_scene_parent and edited_scene_parent.is_ancestor_of(node)

0 commit comments

Comments
 (0)