get_node -> null

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

Hello,
Before trying a HTTPRequest, I want to simply print the object to change it later (TextureRect).

Scene is simple:
enter image description here

And HTTPRequest script even simplier:

extends HTTPRequest
onready var changery = get_node("TextureRect")
func _ready():
     print (changery)

enter image description here

get_parent works though, but I don’t get why get-node is not in this case.
On Ubuntu 21.04

Did you ever have this behavior?

:bust_in_silhouette: Reply From: AlexTheRegent

get_node() starts looking from node that called it. So, if you calling it from script of HTTPRequest, you are looking for childs of HTTPRequest. And since there is no node named TextureRect as child of your HTTPRequest, it returns null. If you want to make this code work, you need to reparent TextureRect to HTTPRequest, so tree would look like

Spatial
  - HTTPRequest
     - TextureRect 

Thanks @AlexTheREgent!
I wouldn’t like to change the tree. Only the path.
You’re right though that without slash it’s a child.

According to Node — Documentation de Godot Engine (4.x) en français
onready var changery = get_node("/root/TextureRect") should work, but it’s not.

j2l | 2021-10-05 14:13

I accept your reply since I can’t honorably accept mine :slight_smile:

j2l | 2021-10-05 14:17

You can also use relative path in get_node like get_node("..") to get parent.

AlexTheRegent | 2021-10-05 15:54

:bust_in_silhouette: Reply From: j2l

Oh, my, I see my mistake
onready var changery = get_node("/root/Spatial/TextureRect")