Godot Version
4.2.1.stable
Question
There seems to be an issue with the loss of references to resources when switching scenes in Godot, especially when these references are set via the @export mechanism in the Inspector.
I noticed that normal strings remain intact in the Inspector, while resources or PackedScenes are lost. For example, when opening a project in Godot 4.2.1 and adding the other scene to each scene (stage1 and stage2) within a Switcher, the reference in the first scene seems to persist after saving and starting, but in the second scene, it does not.
It also appears that after saving and closing Godot, the resources are not properly added, and the scenes cannot be opened anymore. In this case, the faulty scene must be manually switched to another scene in the Dependencies window. It’s also unclear why this is not clearly marked in the Dependencies window.
As a temporary solution, a dictionary was added in the Autoload to associate names with scene paths. Although this solution works, it may not be particularly elegant.
I have uploaded a sample project to GitHub:
https://github.com/KnuTNatioN/buggs
Functional workflow:
In Stage 1 and 2, there is a label (to visibly distinguish where you are) and a Switcher object. Only the Switcher object has code:
extends Node2D
@export var nextScene: Resource
func _ready():
print("Active Scene: ", get_tree().current_scene.name)
print("Next Scene path: ", self.nextScene.resource_path)
func _process(delta):
if (Input.is_action_pressed("next")):
print("Loading next Scene")
MyGlobal.loadNewScene(self.nextScene.resource_path)
After loading this object, the current scene and the potential new scene are output.
The script “myGlobal.gd” is located in the Project Autoload:
extends Node
func loadNewScene(location: String):
if (location == ""):
print("Nothing to change")
else:
print("Loading: ", location)
get_tree().change_scene_to_file(location)
func _process(delta):
if (Input.is_action_just_pressed("esc")):
get_tree().quit()
When a key is pressed, the Switcher object calls the function MyGlobal.loadNewScene() and passes a string, which then switches the scene accordingly. No major security measures, such as checking for invalid strings, have been implemented.
The same problem occurs when passing the entire resource or the PackedScene.
Do I need to add something instead of @export? Or do I need to completely rethink the approach?