Can't get the proper global_position of a child node

Godot Version

4.4

Question

I have a scene with an AnimationPlayer that I want to configure at runtime.

I’m able to set the NodePaths for the tracks to the nodes that exist in my node tree. I also need to set some key values of the tracks to the real positions of my nodes. I tried to get the global_position of my nodes, but I only get the position of the container around the nodes, not for the child nodes itself.

My node tree:

My code to get the global_position of the nodes:

var d1: DiceSlot = enemy_dice_container.get_node("DiceSlot") as DiceSlot
var d2: DiceSlot = enemy_dice_container.get_child(1) as DiceSlot
print(str(enemy_dice_container.global_position))
print(str(d1.global_position))
print(str(d2.global_position))

#result:
#(270.0, 73.0)
#(270.0, 73.0)
#(270.0, 73.0)

I added the children of EnemyDiceContainer at runtime with add_child(), might that be a reason?

Containers take a frame to update the position of their children so if you added them in the same frame you tried to get their global position it won’t be correct.

Thanks a lot for that information! I added

await get_tree().process_frame

before the code snippet and now I get proper results.