Why add_child funtion not work when i used it in _ready functon?

Godot Version

4.2.2

Question

var root_node
var child_node
func _ready() -> void:
	root_node = get_node("../")
	child_node = get_node("child")
	print(root_node)
	
	test()
func test():
	var load_scene = load("res://suika_crane/_test/rigid_body_2d.tscn")
	var ins = load_scene.instantiate()
	#this is not work
	root_node.add_child(ins)
	#this is work
	#child_node.add_child(ins)
func _input(event: InputEvent) -> void:
	if Input.is_action_just_pressed("ui_accept"):
		test()

why add_child not work in this code?
i tested some,and i nortice a few things.

1:add_child not work in ready function.
2:add_child work in input function.
3:add_child work with child node Even when used in ready.

I was able to get the top node without any problems.
Please lend me some help.

maybe add a small delay before you add the child :thinking:
await get_tree().create_timer(0.1).timeout

Unless I am mistaken, I think root_node might be null when you are trying to add the child node. It might have to do with how you are getting the node.

I think that it should be without the forward slash, so root_node = get_node("..")

If that doesn’t work, try using root_node = get_parent() instead?

thanks comment.

var root_node
var root_node2
var child_node
func _ready() -> void:
	root_node = get_node("..")
	root_node2 = get_parent()
	print(root_node,root_node2)
	child_node = get_node("child")
	test()
func test():
	var load_scene = load("res://suika_crane/_test/rigid_body_2d.tscn")
	var ins = load_scene.instantiate()
	#this is not work
	root_node.add_child(ins)
	root_node2.add_child(ins)
	#this is work
	child_node.add_child(ins)

add child not work with get_node("..") and get_parent()
,but i can get top node with both.

shahriarlabib000
thanks comment.
it work well,why this problem?is it specifiation of godot?

Put pass after the last line of function test and set a breakpoint on it and run scene.

Using the debugger at breakpoint to check for any errors in the tab and also click on the scene structure Remote tab to see what your tree looks like while debugger is stopped at breakpoint.

Oh should see the nodes someplace or at least some errors.

idk, probably finding a node that isn’t a direct child takes more time i guess check gertkeno’s reply

The parent is not ready until all of it’s children are ready, you will have to wait or add this functionality to the parent.

For waiting you can wait just a frame with this line

await get_tree().process_frame
2 Likes

thanks ,i get error like this.

thanks i understand about t.

E 0:00:00:0892   test.gd:12 @ test(): Parent node is busy setting up children, `add_child()` failed. Consider using `add_child.call_deferred(child)` instead.
  <C++ Error>    Condition "data.blocked > 0" is true.
  <C++ Source>   scene/main/node.cpp:1420 @ add_child()
  <Stack Trace>  test.gd:12 @ test()
                 test.gd:5 @ _ready()

Can you use preload? If yes, move load before the _ready() method and chage as follows

@onready var load_scene = preload("res://suika_crane/_test/rigid_body_2d.tscn")