When the player clicks I am trying to pass the position and direction of the camera3D (which is in the player scene) to the world scene through a signal to spawn a bullet with that direction and position. The world scene is a parent of the player scene and is able to get the information but only the position actually works, and if I pass position and direction for some reason it won't spawn the bullet in the right place
I tried moving the bullet node around to try to get it to work
The error is telling you that the operation you are doing cannot be done on a
Nodethat is not on the scene tree... Either:Nodehasn't been added yet (i.e. you haven't calledadd_childyet).Nodebeen removed (either because ofremove_childorqueue_free).And since this is about a
Transform3DI'm going to guess this is about reading or writing theglobal_transformof aNode3D.Note: I'll continue talking about the
global_transform, but this applies toglobal_position,.global_rotationandglobal_rotation_degreessince they are all shorthand toglobal_transformfor convenience.Now, setting the
global_transformoutside of the scene tree is not an error, but it setstransforminstead (which would explain why you are getting the bullet spawned in the wrong position).While you could address that by setting the
global_transformafter you added theNode3Dto the scene tree, that might give colliders an opportunity to run before Godot sets the updated position.So, instead I'm going to encourage to set the
transform... Let us say:Node3D, which I'll callinstance.instanceplaced atdesired_global_transform.parentwhich is in the scene tree.Then you do this:
Contrived? Yes, but I'd expect it to work around the wrong position.
The idea is that when
instanceis added as a child ofparent, theninstance.global_transformwill beparent.global_transform * instance.transform... Then theparent.global_transformcancels out with its inverse, leavingdesired_global_transform.But getting the
global_transformof aNode3Dthat is not in the scene tree is an error.In this case you say you read the
global_transformof aCamera3D... Which makes be believe theCamera3Disn't in the scene tree.Since you say the
Camera3Dis part of the player scene, I'm guessing the whole scene is outside the scene tree.You, of course, can use
is_inside_treeto check, and don't run the code if it returnsfalse. If you also add abreakpointthere, it might help you debug the situation.