Godot Version
4.5
Question
So I am once again trying to make metroidvania-style room switching and I got farther than I did last time, but I ran into more weird problems. The script below instances the scene you want to go to while removing the original scene. It works when I load the game for the first time, but it gives me an error when I try to do it later, giving errors related to nodes being nil. Thanks for any help you can give me
Here’s the some of the code for the main scene:
@export var target_scene: String
var current_scene
var current_zoom: Vector2
var current_cam_limits = [-1000, 1020, -1000, 207]
func _ready() → void:
current_zoom = Vector2(2,2)
change_room(target_scene, current_cam_limits, “Left”)
func change_room(target: String, cam_limits: Array, spawn_dir: String):
if current_scene:
current_scene.queue_free()
var new_scene = load(target)
var new_instance = new_scene.instantiate()
var spawn = new_instance.get_node(“Spawns”)
var spawn_node = spawn.get_node_or_null(spawn_dir + “Spawn”)
print(“Spawn node found:”, spawn_node)
if spawn_node and player:
player.position = spawn_node.position
set_cam_limits(cam_limits[0], cam_limits[1], cam_limits[2], cam_limits[3], current_zoom)
add_child(new_instance)
current_scene = new_instance