Why is the scenetree null here?

Godot Version

4.2

Question

I’m new to godot, and to practice in 3D, I’m making a little arcadey flight sim. I couldn’t find any answers for this, though, no matter where I looked.

extends Node3D
@onready var lowpolyfighter = $".."
func _ready():
	pass
var tween = get_tree().create_tween()

scenetree

I have this node, lowpolyfighter, to which this script is linked. It’s a child of a RigidBody3D, which itself is a child of a Node3D. It should be in the scenetree, but it isn’t, and as such it crashes because I’m trying to make a Tween out of something that doesn’t exist. So, what have I done wrong here, and how do I avoid making the same mistake again?

Thanks,
Fishbed.

In your case, since the script is in lowpolyfighter itself, you don’t need to use the @onready var. Just use self instead.

it could work like this, so get_tree should be valid

or

@onready var tween = get_tree().create_tween()

func _ready():
    pass

I couldn’t put it in func_ready because I needed that var in a physics process. Thanks for letting me know about the Onready, I’ll try that.

tried it out, this does work. Now I just gotta work on the other stuff I messed up with the tween. Thanks!

1 Like

I would just like to clarify that @onready and _ready() are exactly the same. You don’t need to worry about your physics process function referencing the tween because it will never be called before the _ready function.

Just wanted to close this up with something I learned while fixing the rest, it turns out you should not call that tween up there with the var and should instead call it in the function itself. If I’m wrong with this bit of advice, please correct me.

Yes, you gotta be careful with “Tween” class. At first you thought it’s just like any other object in Godot, but it comes with several caveats

  • You can’t create it manually just like any other Godot objects, so Tween.new() won’t work.
  • Avoid using more than one Tween per object’s property.
  • Tweens are not designed to be re-used and trying to do so results in an undefined behavior.
  • Etc
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.