'null value' when im trying to switch scenes

Godot Version

4.6

Question

Hello!! When I try to change a scene, it gives me “Cannot call method ‘change_scene_to_file’ on a null value.” Any ideas?

func _process(delta: float) -> void:
	if Globals.minigame1finish:
		get_tree().change_scene_to_file("res://scenes/rooms/meeting_room.tscn")
		Globals.locked = false

Probably not inside the tree, there should be an additional error message saying something like data.tree is null, returning nullptr

So you need to make sure it is inside the scene tree here, but some more details are needed

Make sure the path is right by dragging the scene onto the script.

That’s not the problem here, the problem is that get_tree is null

I don’t understand how you could call get_tree() from a scene and that scene be null?

That isn’t what that does, it’s a method on Node that fetches the current scene, it’s completely unrelated to "res://scenes/rooms/meeting_room.tscn"

If get tree is null, it should show up in a print statement as null. I still don’t understand it but I’ll admit I’m out of my depth here.

Wouldn’t be necessary to confirm as the error is clear

But the solution here would require OP to provide more details

I can see it if the script isn’t attached to a node. If it is, it’s like being on a branch, asking for a tree, and getting an answer there is no tree. Apparently my metaphor isn’t working.

Please check the documentation to understand the way this works instead, this is not the place, OP needs help with an issue and this isn’t the place to teach you how this works (your metaphor is wrong)

Are you sure that’s the line that causes the errror?

Unless there’s a larger file there’s no line that can cause that error, it might be due to some order issue or threading

Yeah, something’s not right here. get_tree() could never return null inside _process(), unless there’s more code there possibly containing awaits.

Do you queue free or end current scene prior to this?

This process call could happen later than you expect. Possibly the next frame after you have run the rest of your game finished code which we dont know what it does.

Just call it by a signal emitted or a function call done when you have achieved what you want. Then you know exactly when it will happen.

That would not cause that, the free queue is not called during other nodes processing, it happens at specific times in the frame

No but i meant that the check that runs in process could have passed already, then turned to true after already giving the results for a false for the current frame and finally get set up to check next frame durikg process. Putting some of tte code to wait around in process while other parts run instantly could be the problem.

It wouldn’t work that way, if we are awaiting then the callback wouldn’t happen as the object is deleted (when freeing a node all signal connections to it are cleared, so the callback won’t happen), so it’s more likely it was removed from the tree but not freed if that’s the case

But queue free is not the cause here I’d say, unless for some reason there’s some await on a signal on another thread that causes that, but that seems very unlikely

Either way the main point is that there seems to be some kind of out of sync behavior from splitting the code up in a weird way. Queue_free() being the exact function called or not called doesn’t matter as much to me as trying to get the code to run when it needs to and not let it wait around until the next process call while other things continue running, changing the things around so that when eventually the change scene runs, it causes the game to break.

First step would be to remove process entirely from the change scene logic and let the things run in the order they need to run.

It’s just guesswork without seeing all of the relevant code, the scene tree setup and the error call stack.

I agree. I could be completely wrong. It just looks very off to me to split it up like this.