Instantiate in editor (using code) doesn't save

Godot Version

4.4

Question

Hi all! I’m trying to get a system where in the editor, using a tool script, I am instantiating 2D nodes. The code runs fine for the instantiation, but the hierarchy doesn’t update, so I can’t save the scene after instantiation. It seems that I need to do some code to set the owner perhaps, or update the hierarchy, but I’m not 100% positive on how this is done.

Instantiate code is pretty simple: “var new_resource = r.node.instantiate()” basically just reading an object that I have (r) and looking at the “node” which is a premade PackedScene that is referenced in the object (r). This works just fine. But saving the scene doesn’t save the “new_resource”

I looked around and found these two lines:
“new_resource.set_owner(resources_node)”
and
“new_resource.owner = resources_node”

but neither seem to do the trick at all. At least, I am not seeing the hierarchy update. Nor saving the scene.

I thought I had read somewhere that you need a line to tell the hierarchy that the scene has updated, but I can’t seem to find info on that. So not sure if its necessary. Anyway, any help here at al would be great! Thanks!

Seems all I had to add was “get_tree().edited_scene_root” and the whole thing works. Good to know for anyone else who needs this!

1 Like