can't instance a scene through code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PrOoOg
:warning: Old Version Published before Godot 3 was released.

i’m trying to instance the bullet scene instead the main scene and i’m having this error:
parent node is busy setting up children, add_node() failed. Consider using call_deferred(“add_child”, child) instead.

the script is attached to the player node, direct child of the scene node.

func _ready():
	var bulletscene = load("res://scenes/bullet.tscn")
	var bullet = bulletscene.instance()
	get_node("/root/level").add_child(bullet)
	set_process(true)
	set_process_input(true)
:bust_in_silhouette: Reply From: YeOldeDM

You should do what the error is suggesting.
get_node("/root/level").call_deferred("add_child", bullet)

Nodes don’t want to add any new children until it’s ready. A node isn’t ready until all its children are ready.

call_deferred takes the function&arguments you give it and puts off calling it until the next process cycle; by then, all the initial nodes in your scenetree should be ready.