Cyclic dependency trick?

Godot Version

v4.3.beta2.official [b75f0485b]


In the game I’m working on I have (among others) two scenes, A and B, that should show something on screen and then switch to each other (start by showing A first, then switch to B a bit later, then switch to A again, then to B etc) and keep switching until the player presses a button.
What triggers the event is a simple Timer scene that looks like this:

extends Timer

@export var next_scene : PackedScene

func start_transition(): # triggered by timer's expiry

So each scene (A and B) has this Timer scene in it where the “next_scene” exported variable is pointing to the other scene… which causes cyclic dependency and corrupts the project.

I’d really like to keep this simple system of switching scenes because it works so well throughout the whole project except in this one instance.

I tried using path names instead of actual scenes in the Timer component’s “next_scene” and this indeed works but it breaks as soon as I move or rename a scene which is impractical.

What would be a good way of making these two scenes point to each other without causing cyclic dependency (if possible using some system that would allow me to not worry about path names)?

There is no proper class for storing the ScenePath.

But you can do it with the Resource class

@export var scene_to_load : Resource

func start_transition(): # triggered by timer's expiry
	var next_scene = load(scene_to_load.resource_path).instantiate()

Its works fine inside the editor.
But you’ll need to build your game to see if this reference holds.

For storing Scene Path, I was using
@export_file var f
which allows me to drag a scene on the exported variable but it doesn’t update if I move the scene around or rename it which is why I dropped it.

I’d expect that using Resources in this situation to cause the same problem as using Scenes (one resource contains links to another and the other way around)? In fact I I tried to implement it and am getting the exact same problem.