Switching rooms using instancing

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

I think your spawn variable is trying to get a node before it exists. That will return nil.

Move the add_child line to above the var spawn line.

Did what you said and it still doesn’t do anything, so what else?

Can you post the error messages? That will help narrow it down.

You also have debug statements in your code. What are they showing?

This code actually doesn’t give me any errors because it is programmed in a way not to do so, and the debug statements were used to try to figure out which nodes were causing the problem, and it seems to be the player node. Here’s the error when I take the safety off:

E 0:00:05:950 change_room: Invalid assignment of property or key ‘position’ with value of type ‘Vector2’ on a base object of type ‘Nil’.
main.gd:33 @ change_room()
main.gd:33 @ change_room()
room.gd:13 @ load_room()
door.gd:10 @ _on_body_entered()

but this is one of those errors that doesn’t really take the full problem into perspective, and just leads to more when I try to fix it.

Suppressing error messages < Fixing what is causing the error

Unfortunately, it seems like we are at an impasse. You aren’t giving people enough details to help solve your problem.

1 Like

Any advice then on figuring out how to find the error myself?

Please format your code by pressing Ctrl+E in the forum editor here and pasting your code into that.

2 Likes

Save your player scene into a global script and instantiate it from there.

1 Like

You can use print statements to watch the player variable in all of your scenes.

Godot has a decent debugger you can use to set breakpoints that allow you to watch variables when the script is running.

Pay attention to the error messages you see and try to resolve each one.