Godot Version
v4.1.2.stable.mono.official [399c9dc39]
Question
I want to add a node to an existing scene.tscn but I can’t get it to work whatever I try the node isn’t getting added to the scene. This is where I’m at:
public override void _Run()
{
Node nodeToAdd = new Node(); // Your node to add
nodeToAdd.Name = "Test123";
AddNodeToScene(nodeToAdd, FindSceneByName("level_01"));
}
public void AddNodeToScene(Node nodeToAdd, string scenePath)
{
var scene = GD.Load<PackedScene>(scenePath);
if (scene != null)
{
var instance = scene.Instantiate();
var sceneTree = (SceneTree) Engine.GetMainLoop();
sceneTree.Root.AddChild(instance);
if (instance is Node sceneNode)
{
instance.AddChild(nodeToAdd);
nodeToAdd.Owner = instance; //needs to be after the AddChild()
PackedScene packedInstance = new PackedScene();
packedInstance.Pack(instance);
ResourceSaver.Save(packedInstance, scenePath);
GD.Print("(AddNodeToScene) Node added to the scene successfully.");
}
}
I already got some help in discord but that want enough to get it to work. The api has nothing useful on this. I’m instantiating the tscn file, then I add it to the scene, add the node to the scene instance, then I pack and save it. All I get is:
Script Started:
File found at path: res:///level_01.tscn
Test: res:///level_01.tscn
File found at path: res:///level_01.tscn
(AddNodeToScene) Node added to the scene successfully.
There is no error not feedback not anything I can work with.
It does show up in the file when I open it:
[gd_scene format=3 uid="uid://c8hco1o8xxplv"]
[node name="@Node3D@28376" type="Node3D"]
[node name="Test123" type="Node" parent="."]
I’ve tried changing it manually to something like:
[gd_scene format=3 uid="uid://c8hco1o8xxplv"]
[node name="@Node3D@28376" type="Node3D"]
[node name="Test123" type="Node3D" parent="@Node3D@28376"]
But that doesn’t make it show the node in the scene view either:

I got the suggestion to run the project for the node to show up but it didn’t work.
I’ve noticed that the [node name=“Test123” type=“Node” parent=“.”] is removed from the file when I run the project.
