How to check for duplicates when using get_parent().get_node()?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pneuma

I am making an onready variable, and I need it to check for a node, however I also need it to check for the nodes duplicates. And since duplicates in Godot have a number after them I can’t just do:

onready var x = get_parent().get_node("x")

So how can I also check for duplicates?

:bust_in_silhouette: Reply From: klaas

an easy way would be to use groups. Put your x-node in a group like “x_nodes”. then get them with

var all_the_x_nodes = get_tree().get_nodes_in_group("x_nodes")

read about it here:

Thanks very much, you seem to be answering all my questions :slight_smile:

I was wondering if there is a way to do this without using groups, since calling a group requires a change in some other code which I dont want to change.

Pneuma | 2020-08-31 10:57

well … in theory you can scan the tree for any instance.

do cou know the call get_node_or_null() with that you can try to get a node

for i to range(0,10):
    var node = get_node_or_null("x"+str(i))
    if node:
        do something( node )

klaas | 2020-08-31 11:05

Ah, it isn’t quiet what I wanted but I’ll figure something out. Thanks!

Pneuma | 2020-08-31 11:20