Back when I was learning Godot, one of the things I’d filed under “this is how it’s done, don’t think about it again” was to modify the nodes after add_child(). Now, I didn’t correctly remember all the subtleties. You are right in that modifying the position before or after you add the node to the tree will produce the same result.
I was wrong when I said the Viewport and the parent node are not accessible while the node is outside the tree. In retrospect, it’s obvious, since you are at this very moment using the parent to add the child, and you have access to your own position.
I was thinking about the way position is relative to the parent, and so if the node doesn’t yet have a parent, how could you correctly set its relative position. That’s not actually a problem (the node knows where it is at all times).
The order does matter, however, when you use the global position:
# Before add_child()
func _ready() -> void:
# Set parent global position (can also use position)
global_position = Vector2(100.0, 100.0)
var node_2d := Node2D.new()
node_2d.global_position = Vector2(100.0, 100.0)
print(node_2d.global_position) # 100, 100
add_child(node_2d)
print(node_2d.global_position) # 200, 200
# After add_child()
func _ready() -> void:
# Change parent global position (relative position also works the same)
global_position = Vector2(100.0, 100.0)
var node_2d := Node2D.new()
add_child(node_2d)
node_2d.global_position = Vector2(100.0, 100.0)
print(node_2d.global_position) # 100, 100
It’s the same for global rotation, global scale and global skew, which I believe get added to the parent’s value when you call add_child(). There’s probably some more to this. I imagine it has the potential to become problematic when multiple nested nodes that change their position are involved (if you mix and match when you modify the position, and which one you use).
As such, wanting to not ever have to think about it, I went with “just don’t change it before you add_child() and it will always work the same way every time”. I suppose the other way is true as well, as long as you are aware of what actually happens in each case.