Godot Version
4.6 stable
Question
How do I properly unload a scene?
So currently I’m working on a scene manager for my game, each time I have a scene that needs to be loaded I instantiate it by sending a signal to the scene manger script. This script will then use the scene_id to identify a specific scene and uses add_or_remove to see if the scene needs to be instantiated or unloaded. Now my problem comes in when I go to unload a script!
At first the script wouldn’t need name_scene = find_child("scene_name", true, true) for id 2-4 (its why its commented out). Being the silly billy I am I just moved on and said “if it works it works” but later on when I would unload the scene more than twice (scene id 2-4) and error would pop up saying:
Attempt to call function ‘queue_free’ in base ‘previously freed’ on a null instance.
After that I just gave up and now we’re here!!! If you have any questions to help you get more details please let me know!
func _on_scene_requested(scene_id, add_or_remove):
if add_or_remove == "add":
match scene_id:
1:
name_scene = main_menu_scene.instantiate()
add_child(name_scene)
2:
name_scene = main_game_scene.instantiate()
add_child(name_scene)
3:
name_scene = pull_up_menu_scene.instantiate()
add_child(name_scene)
4:
name_scene = how_to_play_menu_scene.instantiate()
add_child(name_scene)
elif add_or_remove == "remove":
match scene_id:
1:
name_scene = find_child("main_menu", true, true)
name_scene.queue_free()
2:
#name_scene = find_child("main_game", true, true)
name_scene.queue_free()
3:
#name_scene = find_child("pull_up_menu_scene", true, true)
name_scene.queue_free()
4:
#name_scene = find_child("main_game", true, true)
name_scene.queue_free()
else:
print("bruh _on_scene_changed didn't work")