PackedScene 'Nil' after Scene Reload

Godot 4.4.stable

This is the code I’ve written for door-type transitions within my game:

extends Area2D

@export var target_scene : PackedScene
var transition_timer : Timer

func _ready():
	transition_timer = get_node("TransitionTimer")

# Function called when player enters Area2D Node
func _on_body_entered(body):
	if body.is_in_group("player"):
		open_door()
		transition_timer.start()

# Function to handle opening the door
func open_door():
	$AnimationPlayer.play("door_opening")
	TransitionScreen.transition()
	await TransitionScreen.on_transition_finished
	
# Timer timeout handler for scene change
func _on_tranisition_timer_timeout():
	get_tree().change_scene_to_file(target_scene.resource_path)

So the reason I’ve used @export var target_scene : PackedScene is I assumed that that’d be the best way to duplicate and make many doors of the same sprite with different target scenes by just drag and dropping into the Inspector.

However there’s two issues that I’m running into.

The more pressing one is that whenever I go WORLD - INTERIOR - WORLD, any attempt at re-entering that same interior results in a crash - the target_scene is then expressed as “Nil”, meaning that the Packed Scene has reset itself to blank.

Why is that the case? Am I doing something wrong or should I have used a different method here? Most tutorials / videos I’ve found on the topic are either heavily outdated or not applicable for my exact situation.

The second little thing I’d like to know how to do is store the player’s Vector location before he enters a building so that, when I exit the building, the character is right in-front of the door rather than reset fully to the original location within the scene.

Any help is appreciated!

(Note: this is my first ever game-dev project (JRPG-esque) and I’d love any and all pointers! Feel free to criticize or give any information it’s much appreciated.)

Godot tries to prevent cyclical references by silently deleting them, @export works in a sense like preload in that it must load the assigned value so “WORLD” must load “INTERIOR” which must load “WORLD” which fails to load “INTERIOR” because we’re in a loop.

Ironically I’d say you were half way there, change your target_scene to a path

@export_file("*.tscn") var target_scene: String

You already weren’t using change_scene_to_packed so you only have to remove the .resource_path here.

func _on_tranisition_timer_timeout():
	get_tree().change_scene_to_file(target_scene)
1 Like

Alright! Seems to have done the trick. My thanks!