How to make nodes added with tool script show up in the scene dock

Godot Version

Godot 4.3

Question

`I am using this code to create some nodes and see them in the editor using a tool script

@tool
@export var categories: Array[CategoryRes]

@export var init_cats: bool:
	set(ticked):
		if not ticked: return
		_init_cats()

func _init_cats():
	# Add the categories as children
	for res in categories:
		var cat = Category.new(res.texture, res.title, max_items)
		add_child(cat)
		cat.set_owner(get_tree().get_root())

But when I reload the scene, I get this error


`

I would guess Set/Get style buttons like this could run before the scene tree is fully setup.

You could probably add an ‘is_started’ variable and set to true on ready, then have the init_cats function check this first. (I believe this would be resolved by the new editor button decorators in the 4.4 builds if so)

Also I habitually use “get_edited_scene_root()” rather than get_root(). Could be related, but not at computer to check.

Maybe it’s a similar problem like in this post. You can try to do

if not is_node_ready():
    await ready

somewhere appropriate in your code to make sure the node is fully set up.