How to get all child of nodes in c#?
Asked Answered
R

4

0


I don't want use: node.GetChild(0).GetChild(0).GetChild(0).GetChild(0).GetChild(0).Name
This form of code is not professional.
Thanks.

Radiobiology answered 14/12, 2023 at 7:48 Comment(0)
S
1

Radiobiology some kind of queuing or recursion, perhaps? Seems like you're after a specific node rather than all children so perhaps less than optimal to descend through the tree, although the found node is going to be the terminating condition (or when you run out of nodes). If you know the node you want, why not use FindChild instead (which is recursive) or perhaps GetNodesInGroup (and assign it to a group)?

Perhaps also consider having an NodePath export and dragging the node you seek into the Inspector.

The recursive/queue solution would involved checking GetChildren (with a non-empty result) and then essentially calling the same function (or adding to queue) with the current node as the start point. But I'd avoid and leverage godot's other functions instead.

Sitzmark answered 14/12, 2023 at 10:16 Comment(0)
M
1

Radiobiology I don't use C# but GDScript has find_children. Does that not exist in C#?

Microfilm answered 14/12, 2023 at 10:6 Comment(0)
S
1

Radiobiology some kind of queuing or recursion, perhaps? Seems like you're after a specific node rather than all children so perhaps less than optimal to descend through the tree, although the found node is going to be the terminating condition (or when you run out of nodes). If you know the node you want, why not use FindChild instead (which is recursive) or perhaps GetNodesInGroup (and assign it to a group)?

Perhaps also consider having an NodePath export and dragging the node you seek into the Inspector.

The recursive/queue solution would involved checking GetChildren (with a non-empty result) and then essentially calling the same function (or adding to queue) with the current node as the start point. But I'd avoid and leverage godot's other functions instead.

Sitzmark answered 14/12, 2023 at 10:16 Comment(0)
C
1

Radiobiology What's your actual goal here?

Charkha answered 14/12, 2023 at 11:3 Comment(0)
R
1

Sitzmark Finally fixed with FindChild() Function. Thanks!

Radiobiology answered 14/12, 2023 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.