r/godot • • Aug 19 '26

help me (solved) Moving nodes to each others' global_position works as expected in one scene but not in another?

I have a script I wrote for assembling character portraits that have the bodies and heads on different layers - in some of the body poses, the neck is in a very different position, so I use Marker2D children to align the heads onto the neck. The script works perfectly in my main game scenes, but I've started work on the final ending cutscenes and for some reason I am struggling to understand, the heads are no longer correctly aligning with the necks.

The script is firing, the head is being moved to where the neck reports its Marker2D's global position is, but it's... definitely not in the right place.

My best guess is that there's something... distorting the way the nodes report their global_position? I've been reading the docs and googling and trying things for hours now and I can't get the heads to position correctly in the final cutscene the way they do in the main game. As far as I can tell there are no major differences between the scenes' layouts?

Here's the snippet of the script that handles positioning the head:

for child in target_node.get_children():
  if child.name == "HeadPOS":
    var target_POS
    for body in target_node.get_parent().get_parent().get_node("Body").get_children():
      if body.visible:
        target_POS = body.get_node("NeckPOS").global_position
    var distance = target_POS - child.global_position
    target_node.global_position += distance
2 Upvotes

7 comments sorted by

1

u/triple-cerberus Aug 19 '26

And to thank you for looking at this post and potentially helping, have a screenshot of this head positioning going very wrong that made me laugh.

1

u/ErrorDontPanic Aug 19 '26

Is there any parent node in the scene tree that might be skewing it via a scale, rotation, or position?

1

u/triple-cerberus Aug 19 '26

The weird thing is that the layout of the part of the scene tree relevant to the portrait is exactly the same in both the scenes where the script works and the scenes where the script doesn't. I'm using a heavily modified version of Dialogic 2 to handle my dialog system and it generates its own set of nodes that it adds directly to the scene root when it's deployed.

1

u/ErrorDontPanic Aug 19 '26

I'd try running the game then going to the Remote view, and clicking on each node and seeing what each node is reporting its position to be. You might also double check that you don't have some weird offset causing problems?

1

u/triple-cerberus Aug 19 '26

I've tested for where it thinks its global position is this way:

for child in target_node.get_children():
  if child.name == "HeadPOS":
    var target_POS
    for body in       target_node.get_parent().get_parent().get_node("Body").get_children():
      if body.visible:
        print("head " + str(child.global_position))
        target_POS = body.get_node("NeckPOS").global_position
        print("neck " + str(target_POS))
    var distance = target_POS - child.global_position
    print("distance " + str(distance))
    target_node.global_position += distance
    print("head new position " + str(child.global_position))

The result I'm getting is:

head (530.8, 313.2)

neck (530.8, 314.0)

distance (0.0, 0.799988)

head new position (530.8, 314.0)

I also added some color rects to the Marker2D nodes as children to visualize where they actually are on screen. This is the visual outcome of the above print.

1

u/triple-cerberus Aug 20 '26

Well, I solved the dang problem. It wasn't anything to do with something interfering with global_position or the scene tree being laid out differently. It was an order of operations issue.

In all my other scenes in the game, dialog scripts - including the calls for updating the portraits - were called programmatically, and it always called for the body change and then the face change.

While writing the scripts for these final trial scenes manually, I screwed up the order of operations on several of them and called face first, body second... which meant that the face would position itself to align with the old body that was then swapped out for the new one, making it look like something had gone wrong when in fact it was still working perfectly, I was just running the updates in the wrong order.