Change scene crash

Godot Version

4.2.2

Question

How to fix this error?
When I run current scene (level 1) and go thru a door with get_tree().change_scene_to_file("res://menu.tscn") it takes me to menu. Then click on ‘start’ button with get_tree().change_scene_to_file("res://scenes/level1.tscn")
but at this point then it will crash.

simplify: level1 ->door (in level1 to menu) ->start (in menu to level1) → [CRASH]

whereas, if I start from menu → start (in menu to level1) → door(in level 1 to menu) ->menu → [repeatable]
It would not crash. Can go thru door many times.

Also, how to speed up the change scene, it is slow.

What does the error message say? The time it takes to switch scenes depends on the size of the scenes. You can use background threads and a loading screen to make it more pleasent

Sorry, not error, the debug would not respond

Can you show the code where you are changing the scenes?

I changed it a little, because it suddenly has error, something to do with timer error so I added a Timer node and change from get_tree.create_timer(1) to timer.start(1) and await timer.timeout

Then it cannot read the scene.
Error was
Invalid type in function 'change_secen_to_file' in base 'SceneTree'. Cannot convert argument 1 from Object to String

and now have another error:
Cannot call method 'change_scene_to_packed' on a null value

here is the code for door:

@onready var timer = $Timer
@onready var main_screen = preload("res://menu.tscn") #changed

func _on_lever_is_active():
	#var bodies = get_overlapping_bodies()
	#for body in bodies:
		#if body.name == "Player":
	$AnimationPlayer.play("opening")
	timer.start(1)
	await timer.timeout
	$AnimationPlayer.play("opened")
	get_tree().change_scene_to_packed(main_screen) #changed
	pass # Replace with function body.


func _on_timer_timeout():
	pass # Replace with function body.

and this is for the menu (changed the level 1 → world)


@onready var StartButton = $CanvasLayer/HBoxContainer/MarginContainer/VBoxContainer/Start
@onready var SettingsButton = $CanvasLayer/HBoxContainer/MarginContainer/VBoxContainer/Settings
@onready var QuitButton = $CanvasLayer/HBoxContainer/MarginContainer/VBoxContainer/Quit


func _on_start_pressed():
	get_tree().change_scene_to_file("res://scenes/world.tscn")

func _on_settings_pressed():
	get_tree().change_scene_to_file("res://scripts/options.tscn")

func _on_quit_pressed():
	get_tree().quit()

func _on_start_mouse_entered():
	StartButton.add_theme_color_override("font_hover_color", Color("dae376"))

func _on_settings_mouse_entered():
	SettingsButton.add_theme_color_override("font_hover_color", Color("dae376"))
	
func _on_quit_mouse_entered():
	QuitButton.add_theme_color_override("font_hover_color", Color("dae376"))

@onready and preload do not make sense in a assignment. Choose either @onready and load or just preload

It is still getting an error
Cannot call method 'change_scene_to_packed' on a null value

code for Door:

@onready var timer = $Timer
var main_screen = preload("res://menu.tscn") #changed

func _on_lever_is_active():
	#var bodies = get_overlapping_bodies()
	#for body in bodies:
		#if body.name == "Player":
	$AnimationPlayer.play("opening")
	timer.start(1)
	await timer.timeout
	$AnimationPlayer.play("opened")
	get_tree().change_scene_to_packed(main_screen) #changed
	pass # Replace with function body.


func _on_timer_timeout():
	pass # Replace with function body.

are you sure the path to the menu.tscn is correct?

yes, I dragged it from the file system.

Can you print out the value of “main_screen” before you change the scene?

print("Main Screen: ", main_screen)
get_tree().change_scene_to_packed(main_screen) #changed

you can also try to replace preload with load

You are also starting to play an animation and immediately trying to change the scene here: not saying it is the cause of your problem (I would do as @herrspaten suggested above) but this is certainly strange.

	$AnimationPlayer.play("opened")
	get_tree().change_scene_to_packed(main_screen) 
1 Like

commented out the $AnimationPlayer.play("opened")
and change to var main_screen = load("res://menu.tscn")

Still the same error, null value.

Noticed another error in debugger:
image
line 14 is the get_tree().change_scene_to_packed(main_screen)

Output:
Main Screen: <PackedScene#-9223372009054927648>
Main Screen: <PackedScene#-9223372009054927648>

This indicates that the scene with the script is not inside the scenetree when this is called

the Door node (attached is door.gd) in the world node (/scene)

image

note: Door is not a scene. world and menu are scenes

Do you queue_free the door at any point?

No, did not queue_free the door.

extends Area2D
managed to fix the error and crash. it may have something to do with the timer node and the var load.

Final script.

func _on_lever_is_active():
	
	$AnimationPlayer.play("opening")
	await get_tree().create_timer(1).timeout
	get_tree().change_scene_to_file("res://menu.tscn")
	pass # Replace with function body.

Thank you very much @herrspaten and @pauldrewett

2 Likes

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