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.
- No owner: 6 nodes in the open scene, but after reloading 0 of 3 particle nodes and 0 of 3 lights. The .tscn had only 2 nodes in it, the level and the layer.
- Owner set on each node we added: 3 of 3 particle nodes, 0 of 3 lights.
- The script: 3 of 3 particle nodes and 3 of 3 lights.
- 3 instances of a saved torch scene with a flame and a light inside, owner set on every node inside them as well: 12 nodes inside the 3 torches after reloading instead of 6, and freeing that loaded scene left 6 nodes behind outside the tree. The .tscn had 11 nodes.
- The script on the same torches set only the 3 instances: 6 nodes inside, nothing left behind, 5 nodes in the .tscn.
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
- Call it after add_child(), so the scene root is already above the node.
- Its return value is how many owners it set, handy to print while you test your tool.
- Placing many things with even spacing: even brush spacing. Along a curve: objects evenly along a Path3D. One emitter for a whole level of points: ambient particles from emission points.
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
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame