Plugin add_child doesn't show children in the inspector

Godot Version

4.2

Question

I have a plugin that adds some needed nodes as children when creating that node. For example, creating an Area2D will add a CollisionShape2D with a RectangleShape2D as the shape. Here’s a simplified version of my code:

func on_node_added(node: Node):
	# node isn't created, just displayed on scene load
	if node.get_child_count() > 0: return
	if node is Area2D:
		var new = Node2D.new()
		node.add_child(new)
		new.owner = node

This works both when creating a new scene and when adding a node to a scene, but when the node is created in the scene, the child node doesn’t show up in the inspector.
Here’s a tree of this example:

scene_root
|
___ new_node       // no child shown here, but exists when checked with `get_children`

And here’s creating it on a new scene. Notice the added node as a child.

new_node         // (scene root)
|
____ created_child_node // shows up here

Any ideas as to why this happens, and how I can fix this?

Could you be having this issue?

The Node.owner must be the root node of that scene. You can use EditorInterface.get_edited_scene_root() to get it: new.owner = EditorInterface.get_edited_scene_root()

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.