has_node() return true but find_child() return null?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By qdmyxwlhx
            if subCate0.has_node("craft_item"):
				item0=subCate0.find_child("craft_item")
			if not item0:

has_node return true so code run second line successful. but find_child return null which enter the next if block

Need to be mentioned is that this code is in a loop(for) block, the first time run it was works. In the scene_tree l post below, it handle the node named throwingweapon(variable cateSub0) and cobble(named craft_item before)

Would get_node not acheive what you are trying to do. I looked in the Node documentation and couldn’t find the find_child function.

godot_dev_ | 2023-01-13 18:34

@godot_dev - note this is a Godot 4-related question.

Node — Godot Engine (latest) documentation in English

jgodfrey | 2023-01-13 19:30

What does your scene tree look like? Specifically, what node is the above script attached to and where is the craft_item node with respect to it?

jgodfrey | 2023-01-13 19:37

The script attach to an Autoload node so is the child of root. Craft_item is the child of subCate0, they are the part of the UI “desk_panel”. Desk_panel’s path:“root/Main/CanvasLayer/UI/desk_panel”

desk_panel.print_tree() look like(var subCate0 here named weapon):

╴desk_panel
┠╴@@5
┖╴tool
┠╴VSplitContainer
┃ ┠╴throwing_weapon
┃ ┃ ┠╴Label
┃ ┃ ┖╴cobble
┃ ┠╴weapon
┃ ┃ ┠╴Label
┃ ┃ ┖╴craft_item

qdmyxwlhx | 2023-01-14 03:29

:bust_in_silhouette: Reply From: qdmyxwlhx

l read the doc and find out the reason! l change the code like:

item0=subCate0.find_child("craft_item",true,false)

And it works. The thrid arg is owned, doc:

 if owned is true, this method only finds nodes who have an assigned Node.owner.

thx everyone here :slight_smile:

1 Like