global_position property for a node created in gdscript is always (0, 0, 0)

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

Why does global_position always return (0, 0, 0)?

func create_new_target_area():
    var target_area: Area3D = Area3D.new()
    var area_shape: CollisionShape3D = CollisionShape3D.new()
    area_shape.shape = CylinderShape3D.new()
    area_shape.shape.height = 2.0
    area_shape.shape.radius = 0.5
    target_area.add_child(area_shape)
    
    target_area.set_global_position(Vector3(1, 1, 1))
    get_parent().add_child(target_area)
    print(target_area.position) # (1, 1, 1)
    print(target_area.global_position) # (0, 0, 0)

func _physics_process(delta):
	target_area.set_global_position(Vector3(1, 1, 1))
	print(target_area.position) # (1, 1, 1)
	print(target_area.global_position) # (0, 0, 0)

Changing the global position assignment to after add_child() does not make a difference. I thought it might have something to do with tree initialization order, but setting global_position every frame in _physics_process yields identical results.

Curiously, changing

get_parent().add_child(target_area)

to simply

add_child(target_area)

makes everything work as one would expect.

Thank you for any insight you may be able to offer.

:bust_in_silhouette: Reply From: killa_fr0gg

Alright, for posterity, this seems to have something to do with scene tree initialization order.

Disabling physics processing, connecting the function to the parent’s ready signal, and enabling physics processing again in the function is a working solution. The order in which the global_position is set vs the node being added to the parent makes no difference.

The initial output still makes no sense to me, though, and I would like it if someone could point out the specific engine quirk that is making that happen.