← Pixel Dreamer Studio

Nodes your editor tool adds that stay in the scene

Set the owner of each added node and of the nodes under it, but stop at an instanced scene: set only the instance itself.

A node you add from a @tool script or an editor plugin shows in the scene tree, but the scene file only keeps nodes whose owner is the scene root, and add_child() does not set one. In our Godot 4.7 test, 3 particle nodes with a light under each were added without an owner: after saving the scene and loading it back, 0 of 3 were left. Setting the owner on the 3 nodes we added kept them, but lost the 3 lights under them. The script below sets the owner on a node and everything under it, but not inside an instanced .tscn: doing that too saved the insides of 3 torch scenes twice, 12 nodes where there should be 6.

The code

Save it as keep_in_scene.gd and load it with const KeepInScene := preload("res://keep_in_scene.gd"). In your tool script or plugin, right after parent.add_child(node), call KeepInScene.own(node). In the editor it uses the scene you are editing as the owner. Outside the editor, pass the scene root as the second argument, or it does nothing.

extends RefCounted
## Nodes that a @tool script or an editor plugin adds are only saved with the scene when their owner is the scene root.
## Without it they show up in the editor, then are gone after you save, close and reopen the scene.
##   const KeepInScene := preload("res://keep_in_scene.gd")
##   parent.add_child(node)
##   KeepInScene.own(node)   # right after add_child


## Gives `node` and the nodes under it an owner, so they are saved. Returns how many nodes it set.
## root: the scene root. Leave it empty in the editor to use the scene you are editing.
## A node that is an instance of another .tscn keeps its own insides: only the instance itself is set.
static func own(node: Node, root: Node = null) -> int:
	if root == null and Engine.is_editor_hint():
		root = node.get_tree().edited_scene_root
	if root == null or node == root:
		return 0
	var count := 0
	var todo: Array[Node] = [node]
	while not todo.is_empty():
		var n: Node = todo.pop_back()
		n.owner = root
		count += 1
		if n.scene_file_path.is_empty():
			todo.append_array(n.get_children())
	return count

What we measured

A level built in code, with nodes added the way a tool script adds them, then saved with PackedScene.pack() and ResourceSaver.save() to a .tscn file, which is what saving a scene does, and loaded back. Two runs gave the same numbers.

Why it happens

Saving packs the scene root and every node whose owner is that root. Nothing else goes into the file, even if you can see it in the editor. add_child() leaves the owner empty, because the new node could belong to another scene. The nodes inside an instanced .tscn are owned by that instance and saved in its own file. Make the outer scene their owner and the outer file saves them a second time, next to the copy that the instance brings.

What to change

Paint particles in the editor: VFX Painter Kit for Godot 4 lets you paint ambient particles onto your level in the editor, with each layer as one emitter and one draw call, and 12 presets from fireflies to ground mist.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: VFX Painter Kit for Godot 4 lets you paint ambient particles onto your level in the editor, with each layer as one emitter and one draw call, and 12 presets from fireflies to ground mist.

More how-tos for VFX Painter Kit for Godot 4: Ambient particles in many places from one emitter · Place brush dots at the same spacing however fast the mouse moves

More how-tos: Game systems