This code wont work

Godot Version

Godot 4

Question

heres my code

extends Node3D

var parent = get_parent()

func _ready():
	pass 


func _process(delta):
	print(parent)

this returns <Object#null>

but if i do it like this:

extends Node3D

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var parent = get_parent()
	print(parent)

it returns this:
watermelon:<Node3D#31071405353>

how do i get it to return that but establish the var outside of the functions

nrvm i use @onready

Node.get_parent() returns the parent of the node or null if there is no parent. When the script initializes the variables the node has yet to be added to the SceneTree so get_parent() will return null.

Use @onready to delay the initialization of the variable until the node is ready.

1 Like