Instantiating by code and editor

Godot Version

4.2.1

Question

I feel like having a basic knowledge gap, which causes recurring issues. In some cases doing stuff by editor and reproduce it by code is not that easy because you dont know what editor is doing for you.

Like instantiating a node by drag and drop into the scene tree ontop of another node and makes it a child of it. So from now on the child node stick on its parents position and gets it updated automatically in case of animations and stuff.

Now if you want to reproduce it by code:

var child = load("res://child_node.tscn").instantiate()
parent_node.add_child(child)

That not behave same as in the editor. I have hard times even get its position some times, especially if it comes to animations and updating the current position. Why is that the case, what is happening under the hood of the editor.

Not 100% sure what you are asking, but the set_owner property might help you.

When you use it, the node you added in code will appear in the tree. Also be sure to use @tool in your script or it won’t even run in the editor.

Most of the things that editor does can be replicated by code. The thing is that those actions that you described takes different steps.

For example, just instancing through editor using the chain :chains: button steps are pretty similar to what you did:

var child = load("res://child_node.tscn").instantiate()
parent_node.add_child(child)

This is the previous code + getting the mouse position. Since it was a drag&drop action and editor is built with control nodes, it has a function to know where the node was dropped, and the editor solves the position between the position in the screen and the viewport.

This is a different task :thinking: What are you trying to do?

This case hopefully makes it clearer:

Inside Editor: Create a BoneAttachment3D and choose specific bone. Now attach any node as child node to it. The result is, the attached node moves with the BoneAttachment3D Node, even with animations.

Inside Code: If recreating the same by code, attached nodes, wont move with the parent node (BoneAttachment3D).