# access a node's child by name, not path or index

**URL:** <https://forum.godotengine.org/t/access-a-nodes-child-by-name-not-path-or-index/13106>\
**Category:** Archive\
**Created:** [December 1, 2020, 6:54am UTC](https://forum.godotengine.org/t/access-a-nodes-child-by-name-not-path-or-index/13106 "2020-12-01T06:54:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forum.godotengine.org/uploads/default/original/2X/4/4b0a7fa8a6334634b94a13d421f0d99e6f04e21f.svg) [@system](https://forum.godotengine.org/u/system)\
**Post date:** [December 1, 2020, 6:54am UTC](https://forum.godotengine.org/t/access-a-nodes-child-by-name-not-path-or-index/13106/1 "2020-12-01T06:54:20Z")

</div>

| | | |
| --- | --- | --- |
| ℹ | **Attention** | Topic was automatically imported from the old Question2Answer platform. |
| 👤 | **Asked&nbsp;By** | witch\_milk |

I want to access a node’s child using that child’s name and NOT it’s path or it’s index if that is possible.

for example:  
node.get\_child(child’s\_name\_no\_quotes).queue\_free()

or  
var player = get\_parent() #acquire the desired name

name = player # change the name of a child in a different node to be the same as the player, like giving it an ID that is special and accessible from other nodes using it’s ID.

one thing that also bugs me is that when you use get\_node, you have to put the argument in quotes, and the argument has to be a node path?. wouldn’t that make it impossible to put a variable in quotes without it turning into whatever was quoted?

---

<div class="post-metadata">

**Author:** ![system](https://forum.godotengine.org/uploads/default/original/2X/4/4b0a7fa8a6334634b94a13d421f0d99e6f04e21f.svg) [@system](https://forum.godotengine.org/u/system)\
**Post date:** [December 1, 2020, 8:28am UTC](https://forum.godotengine.org/t/access-a-nodes-child-by-name-not-path-or-index/13106/2 "2020-12-01T08:28:30Z")

</div>

| | | |
| --- | --- | --- |
| 👤 | **Reply From:** | avencherus |

Not exactly.

[get\_node()](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-get-node) takes a path which is a [NodePath](https://docs.godotengine.org/en/stable/classes/class_nodepath.html) type, which behaves like a String.

The closest thing to what you want is the shorthand $, but it will need quotes if you use spaces in your node names.

`$my_node` or with spaces `$"my node"`

For manipulating paths with variables you concatenate as you need.

```auto
for i in 10:
	get_node(str("node_", i))

```

If you want to use some other method you can collect up the node references and store them in a Dictionary, using the key as the identifier you prefer.

```auto
onready var node_table = {
	
	1 = get_node("thing"),
	2 = get_node("thing/child_1"),
	2 = get_node("thing/child_2")
}

```

You could also create constants with the paths, so these can be updated quickly from the top of your script.

```auto
const FIRST = "thing_1"
const SECOND = "thing_1/child_1"

if(has_node(FIRST)):
	get_node(FIRST)

```

Nodes also instance IDs, but these are entirely fixed as you develop, and the tracking of these can be more verbose and prone to error.

Objects have the method [get\_instance\_id()](https://docs.godotengine.org/en/stable/classes/class_object.html?highlight=object#class-object-method-get-instance-id).  
GSCript has the utility function [instance\_from\_id](https://docs.godotengine.org/en/stable/classes/class_@gdscript.html#class-gdscript-method-instance-from-id).

thanks for the response.  
Is it possible to use get\_node() when I want to access an instanced child?

```auto
func _on_Area_area_entered(area):
    var a_name = area.get_parent().get_parent().name #the parent of the area
    print(a_name) #would print: Player
    if $'a_name': #trying to search children with the same name as area.
    pass #already have a child with the name
   else: #don't have a child with the name
      var new_child = instanced_node.instance()
      self.add_child(new_child)
      new_child.set_name(a_name)    

```

beginner, very lost here sorry

witch\_milk | 2020-12-01 17:22

Just a quick correction on the “Dictionary” solutions:

```auto
onready var node_table = {

    1 : get_node("thing"),
    2 : get_node("thing/child_1"),
    3 : get_node("thing/child_2") 
}

```

Changes: “=” (assign) changed to “:”, numeration was wrong (1,2,2 changed to 1,2,3)

Might seem dumb but could help newbies

Gianclgar | 2022-05-15 21:47
