Issue with cyclical reference

Hello!

I have bricked my project by accidentally having two objects reference each other

I have a pen without a cap and a pen with a cap, they both can transform into each other and thus have eachother’s scenes as a var PackedScene = load in their code
This caused Godot to completely brick itself until i removed said scenes

Is there a way to have object A reference object B while object B references object A?

Thank you!

The easiest solution (IMO) is to refer to the scenes by string and load (NOT preload) at runtime.

Run time loading: Do not load with var or const at the top of the script, but dynamically load when needed:

# Avoid this
var other_scene = load("res://other.tscn")

# Use this
func transform():
    var other_scene = load("res://other.tscn")

Or use preload (there will be no circular dependency issue):

var other_scene = preload("res://other.tscn")