Why does the order of adding child and setting position matter?

When we’re making the bullets a child of the container node, our script looks like this:

func shoot():
	var rocket_instance = rocket_scene.instantiate()
	rocket_container.add_child(rocket_instance)
	rocket_instance.global_position = global_position
	rocket_instance.global_position.x += 75

However, it doesn’t work if we set the global position before adding it as a child of the rocket_container i.e.

func shoot():
	var rocket_instance = rocket_scene.instantiate()
	rocket_instance.global_position = global_position
	rocket_instance.global_position.x += 75
	rocket_container.add_child(rocket_instance) # Add as a child last

I’m curious, why does the order matter?

my understanding is that when a bullet is instantiated, its not part of the scene tree at that point, and doesnt have any transform information.
so calling add_child places it in the tree and we can then set its transform information.

seemingly, there is a post regarding it on the godot github pages that it resets positional information if set before add_child.

2 Likes

Thanks. That would explain the behaviour, I’ll use this info to look into it more!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms