How to copy global_position with referncing it?

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

In Godot, how do I copy the global_position of NodeA to NodeB so when I make changes to the global position of NodeB, it doesn’t change NodeA?

func _ready():
    var otherNode =  get_parent_get_child(4)
    global_position =otherNode.global_position
    global_position.y += 500 #changes otherNode position
:bust_in_silhouette: Reply From: jgodfrey

You just assign it. So, this:

$NodeB.global_position = $NodeA.global_position

That’s just assigning the current value - there’s no reference there. So, a later change to NodeA’s position won’t be reflected in NodeB’s position.

If you’re seeing something different, there’s more to your situation that’s not obvious (like, maybe the nodes in question have a parent/child relationship?).

I edited the question with more information

shifitzel | 2022-08-30 17:07

That should not be the case. Are you sure that otherNode is referencing the node you expect (that get_child(4) reference is pretty brittle).

I assume that either:

  1. Your node reference is wrong (and both references in your script point to the same node) or…
  2. The nodes in question have a parent/child relationship so that changing the position of the parent also changes the position of the child.

jgodfrey | 2022-08-30 17:23

Neither is the case, the node reference is correct and there is no parent/child relationship.

shifitzel | 2022-08-30 18:21

Hmmm… It really doesn’t work that way, so something else is at play here. Is the project available for inspection?

jgodfrey | 2022-08-30 18:25

I can send you a copy if you want.

shifitzel | 2022-08-30 19:14

Sure, if you want. I just set up a disposable email address for this purpose. It’s only good for 1 hour.

jgodfrey | 2022-08-30 19:25

I sent the project

shifitzel | 2022-08-30 19:34

Hmmm… I didn’t receive it (yet). Can you verify the address you used? I did just cut/paste the address as listed above and was able to send an email with attachment to it…

jgodfrey | 2022-08-30 19:55

I’ve updated the posted address above. And, it is valid for however long, but any email sent to it will only last for 1 hour before being auto-deleted.

If you have another way to make the project available to me, that’s fine too.

jgodfrey | 2022-08-30 20:19

I sent it to you

shifitzel | 2022-08-30 20:31

Yeah, I’m not sure why that’s not working, but I’m not receiving anything from you. Maybe the attachment is too big?

You could try just sending a test email without the attachment. If that works, you could upload the project somewhere and send me a link to it via the provided email address (assuming you don’t want to provide public access).

Again, I’m happy to take a look, but you’ll need to make the project available somehow I guess…

jgodfrey | 2022-08-30 20:44

Download MyGame by shifitzel -

Here is the link. When you start the project, hit the bird. The bombs that the bird drops change its global position. Thanks for helping me!

shifitzel | 2022-08-30 20:47

Alright, I have the project but I’m not sure what you’re referring to. I assume the code in question is in the _ready() function of the script, right?

If you add a print() statement after assigning the global_position there, you’ll see that changing the value of one does not change the value of the other - as expected. So, something like this:

var otherNode

func _ready():
	otherNode = get_parent().get_child(4)
	global_position = otherNode.global_position
	global_position.y += 500
	print("Ready: %s, %s" % [otherNode.global_position, global_position])

Note that I’ve made otherNode global so I can reference it from a print() statement in _process() also as below:

func _process(delta):
	print("Process: %s, %s" % [otherNode.global_position, global_position])
	velocity.y += 2000 * delta

That outputs data like this:

Ready: (1500.857422, -810.982971), (1500.857422, -310.982971)
Process: (1467.526855, -1000.005615), (1500.857422, -310.982971)
Process: (1450.861572, -835.48114), (1500.857422, -146.411224)
Process: (1434.196289, -834.930908), (1500.857422, -145.855713)
Process: (1417.531006, -833.820618), (1500.857422, -144.74469)
Process: (1400.865723, -832.154175), (1500.857422, -143.078156)
Process: (1384.200439, -830.487732), (1500.857422, -140.85611)
Process: (1367.535156, -828.821289), (1500.857422, -138.078552)
Process: (1350.869873, -827.154846), (1500.857422, -134.745483)
Process: (1334.20459, -825.488403), (1500.857422, -130.856903)
Process: (1317.539307, -823.82196), (1500.857422, -126.412811)
Process: (1300.874023, -822.155518), (1500.857422, -121.413208)
Process: (1284.20874, -820.489075), (1500.857422, -115.858093)
Process: (1267.543457, -818.822632), (1500.857422, -109.747467)
Process: (1250.878174, -817.156189), (1500.857422, -103.081329)
Process: (1234.212891, -815.489746), (1500.857422, -95.859688)
Process: (1217.547607, -813.823303), (1500.857422, -88.082535)
Process: (1200.882324, -812.15686), (1500.857422, -79.74987)
Process: (1184.217041, -810.490417), (1500.857422, -70.861694)
Process: (1167.551758, -808.823975), (1500.857422, -61.418003)

This seems to be working as I’d expect. And, since I don’t really understand the game, I’m not sure what I’m looking for visually.

jgodfrey | 2022-08-30 22:50

I changed the position of the bomb to be farther away from the bird and it works. I think when the bird touches the bomb it brings the bird lower. I still don’t know why though. I’m sorry you had to go through all this trouble, it had nothing to do with changing the global_position. Thank you for your help!

shifitzel | 2022-08-31 00:19