Trouble with switching scene back to main menu

So far my project looks like this:
A “main menu” scene that has a button, which swaps the scene to a level.
when you die in the level a death menu pops up.
The retry button works perfectly fine, but the go to main menu button does not work, and only prints “go to main menu”

image

Death menu script:

extends Control

var level = preload("res://All scenes/Level.tscn")
@export var main_menu:PackedScene

func _on_retry_pressed():
	get_tree().reload_current_scene()

func _on_go_to_menu_button_pressed():
	get_tree().change_scene_to_packed(main_menu)
	print("go to main menu")

Have you setted the main_menu variable in the inspector? Check if you debugger isn’t printing a message like that:

1 Like

export and preload can create a cyclical dependency, try using change_scene_to_file with a string path instead.

@export_file("*.tscn") var main_menu_path: String

func _on_go_to_menu_button_pressed():
	get_tree().change_scene_to_file(main_menu_path)
	print("go to main menu")

To expand on this: If script A preloads or @exports script B, and script B preloads or @exports script A,     (A ⟺ B) Godot will be unable to load one of them and as of 4.3 it silently fails. Generally changing scenes is better done by change_scene_to_file, where change_scene_to_packed is great for loading runtime created scenes.

1 Like

I probably should’ve included that in the original post but yeah, I did set it correctly.

Can you show the contents of the scene assigned to main_menu?

Thanks! it works now!