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).

Hello Thomas,

I’m facing basically this issue that you described:

I create a BoneAttachement3D in the editor = all good
I create a BoneAttachement3D in the code = it seems to be fine, but when I check its GlobalPosition, it’s wrong (hence, anything I make child of it it’s displayed in the wrong position).

BoneAttachment3D boneAttachment = new BoneAttachment3D();
boneAttachment.BoneIdx = boneId;
skeleton.AddChild(boneAttachment);

I tried using the Ready event (to make sure when I check the GlobalPosition it’s all loaded) but it’s the same.
Any idea what the editor is actually doing? I’m planning to report this as a bug, as this is the 3rd forum I’m asking and nobody knows :-/

Thanks!

1 Like

Probably useful to report, i didn’t found any solution

Thanks for the quick reply. I guess I’ll report it and if I find a solution will come back here.

Post the link pls

Here:
Add BoneAttachment3D programmatically gives wrong GlobalPosition · Issue #92813 · godotengine/godot (github.com)

1 Like

Hey, I solved this!

I wrote the answer here, last post(s):