Godot Version
4.3
Question
I am new to Godot (1 week since I started) and after a few tutorials I tried to learn by creating my own project.
Right now, my Main loads a MarginContainer representing my main menu
The script of this container lets me launch a new game by switching scenes :
extends Control
const game_scene: PackedScene = preload("res://game.tscn")
[...]
func _on_label_new_game_pressed():
var main = self.get_parent()
var new_game_scene = game_scene.instantiate()
main.add_child(new_game_scene)
At this point, I want to be able to return to the main menu via a similar method (script in the game node) :
extends Node
@onready var main_menu: PackedScene = preload("res://main_menu_test.tscn")
func _ready() :
print(main_menu)
var test_scene = main_menu.instantiate()
print("Test instantiation:", test_scene) # Should print the instantiated object, not null
if test_scene:
get_tree().root.add_child(test_scene)
func _on_quit_game_button_pressed() :
var main = self.get_parent()
var new_game_scene = main_menu.instantiate()
main.add_child(new_game_scene)
call_deferred("queue_free")
However the previous code can’t change the scene, and through debug in _ready(), it seems that main_menu can’t be instantiated.
I feel like there is something I didn’t get, maybe a problem with queue_free() but I can’t find how to make it work while removing the main menu scene.
Is there a better method to do that ?