How to access a node?

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

I need to access node in my game in script game, but I don’t know, how I can do it

You’re going to need to provide some more information here. Minimally:

  • What does your scene tree look like?
  • What node in the scene tree are you trying to access (the target node)?
  • What node in the scene tree needs to access the target node?

jgodfrey | 2023-04-28 16:24

:bust_in_silhouette: Reply From: TRAILtheGREAT

There are a few different ways to go about it:

You can right click on the node you want to access and select “Access as Unique Name”. A % sign will appear next to the node in the scene tree and you can now type %NameOfYourNode to access it in a script. This only works for scripts that are attached to nodes in the same tree as this node, so if you reuse your script elsewhere, you either need to make sure that the unique named node is present or use a different method for accessing nodes that won’t throw errors.

Another method would be to add @export var : Node to the top of your script. This will create a field in the inspector where you can select a specific node in your scene in the editor. This is useful if you want to frequently change the node you are accessing while designing your game.

Another method, which I don’t recommend you make frequent use of, is to use get_node("/root/path_to_the_node"). This allows you to access a node from anywhere as long as you know the path to that node from the root of the scene. Similarly, get_tree().get_root().find_node("node_name") allows you to get any node from anywhere, even if you don’t know the path, just the name. Again, I don’t recommend you use these methods often as they are slower than other methods and more importantly lead to messier and less predictable code.

There are also a couple of specialist ways to access specific nodes:

node_name.owner will give you that node’s owner, which is the node at the top of its scene tree. Importantly, if you save that scene tree and then instantiate it in another scene, the owner will remain the same.

node_name.get_parent() will give you the parent of a node, the node which it is listed under in the scene tree.

This documentation page also lists a lot of other ways to get access to a nodes children, like if you wanted to iterate over a nodes children, or just access a child by name.

All of this is stuff you will learn in basic Godot and GDScript tutorials, so I recommend you find one and read/watch it to completion. A lot of this can also be learned by searching for it in the documentation. Whenever you have a question, its always good to do a cursory google search to see if anyone has already asked the same question of if there is an article which explains what you’re trying to do. Forum lurkers like me are happy to help you when you run into problems, but we aren’t good at answering the same questions over and over again, that’s what computers are for. I wish you luck in your Godot journey, have fun coding!