+1 vote

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.

Godot version v3.4.2
in Engine by (51 points)

1 Answer

+1 vote

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.

by (29,090 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.