How to get the local position of an object as if it was a child of another object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By even_nuller

So basically I have this Position2D and I want to get the local position and add a wall there from a TileMap but it’s a child of an object so just getting the local position won’t work and getting the global position also doesn’t work because the object is in a scene that would be instantiated in another scene so the position that it would get is offseted from the position that I want. And I can’t set it to be the child of the scene since there will be multiple of the same so it would take a long time to get it.

:bust_in_silhouette: Reply From: Zylann

You could first convert the position of the object to global, and then back to local, using to_global and to_local functions of Node2D.

For example:

var global_pos = nodeA.to_global(position_local_to_nodeA)
var position_local_to_nodeB = nodeB.to_local(global_pos)

If you can’t get the global position I’m not sure what you could do. Because you really need to know which transformation to apply to convert from “local to node A” to “local to node B”, and that can’t work if you don’t have that information. Sounds like you need to untangle that problem first. Maybe change your structure or the way information is passed around.

Thanks for your help. I just had to look up what to_local does and then I had to call it on the scene node (Couldn’t use get_tree().current scene had to use get_parrent() to get it instead) and then put in the objects global position and then it worked flawlessly

even_nuller | 2022-01-31 14:06