Avoid Cyclic References with PackedScenes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By figtickler

I have a script to changes the scene when a button is clicked. Because the scene that holds this script is preloading a scene that preloads the first scene, I get this error:

E 0:00:01.795   load: Resource: 'res://scenes/CityStreet.tscn' is already being loaded. Cyclic reference?

Is there a way to be able to reference scenes to each other without creating a cyclic reference?

The script in question:

extends TextureButton

export var next_scene_path: PackedScene

func _on_pressed():
	print("button pressed")
func _get_configuration_warning() -> String:
	return "No scene loaded" if not next_scene_path else ""

func transition() -> void:
	var next_scene = next_scene_path.instance()
	get_tree().get_root().call_deferred("add_child", next_scene)
:bust_in_silhouette: Reply From: luislodosm

If you have two scenes referencing to each other with export var scene: PackedScene, you will have a cyclic reference error.

A solution is to use export (String, FILE) var scene_path.

To keep it handy, I use a custom class to store the scene path.

extends Resource
class_name ScenePath

export (String, FILE) var value