Global position of same Node2D varies between scripts

Godot Version

4.4.1

Question

So I have been trying to access the global position of a Node2D in my game, for now I have just been printing out the value with print_debug to see the issue.

In one script, I have been doing print_debug(pedastal_a.card_slot.global_position), where card_slot is the Node2D in question. This script is attached to a Node2D that is a child of a CanvasLayer of my main scene.

In another script, I used print_debug(home_pedastal.card_slot.global_position) which is effectively referencing the same Node2D since home_pedastal is pedastal_a, just with a different variable name. This script is attached to a component of a Node2D that I will instantiate into the scene under the same CanvasLayer parent as the previous script I mentioned.

The problem is that these two scripts each print out a different global_position. The global_position that is printed out in the second script is the desired value, but I have not yet found a way to access this value in the first script.

For some more context, the node2D in question does not change position, and when testing I have ensured that I haven’t moved it in any way between the printing of the two. If something is different I guess it would be the time at which the global position is accessed, in which case the second script accesses it later.

I fixed the issue, it was indeed related to the timing at which I accessed the global_position of the Node2D. If you don’t wait and access it immediately upon starting the game, it returns an undesired value. To fix this, I just added call_deferred to the function in script 1 to make it wait until the next idle frame. It then correctly returns the global_position.

1 Like

“Ah, good to know it was just a timing issue! I’ve run into the same problem before where global positions aren’t updated yet on ready. call deferred is a lifesaver in those cases. Thanks for sharing the fix!”