#Very weird behavior with loading back a scene, I isolated the problem but still can't make sense

5 messages · Page 1 of 1 (latest)

peak matrix
#

I was encountering some weird problems while loading from a scene to another and then back, so to check my sanity I made a minimal reproducible example of the problem.

My minimal example is the following.
Two scenes, that are visually distinguishable, they both have at their root a copy of this script, with "next_scene" set to the other scene.

extends Node2D

@export var next_scene: PackedScene = null

func _process(delta: float) -> void:
    if Input.is_action_just_pressed("space"):
        print(next_scene)
        get_tree().change_scene_to_packed(next_scene)

Expected behavior:
I would be able to navigate from one to another back and forth.

Actual behavior:
First load goes well, then going back fails because "next_scene" is said to be null

This happens no matter which scene I start with, proving that it isn't my setup that is wrong.
I tried to implement my own scene manager that would instantiate and queue_free() nodes instead, but I get IDENTICAL behavior.
At this point I'm losing my mind as I've been at it for a few hours snailcat.

#

Very weird behavior with loading back a scene, I isolated the problem but still can't make sense

peak matrix
#

The packed scenes in each scenes referring to one another results in cyclic dependencies, which breaks everything.
My solution is to use a Resource that stores the path of the scene instead of PackedScenes.
That way, if I change the location of a scene, I only have to change it in one other place.

earnest merlin
#

hey! thanks for this - I'm having the exact same problem and it is baffling. Will try your solution, although it makes me kind of sad since export packedscenes are so nice and tidy

#

cyclic dependencies make sense. Thought they changed the interpreter with gdscript 2 so that this wasn't a problem anymore. alas.