so I made a door system and when you enter a door, you switch from room,For that I use door nodes(I can do that thanks to class_name) + I have an export variable called “next_room”.
When I have more than 1 door in 1 room and the doors don’t have the same room/scene packed in next_room, then on door1 everything goes fine. It teleports you to thee selected room, but if you enter door2. It will not teleport you to the other room, but will give you this error: “Cannot call method ‘instantiate’ on a null value.”
class_name Door
extends Area2D
@export var next_room: PackedScene = preload("res://areas/Widow's Cradle/normal rooms/room_1_1.tscn")
@export var player_position: Vector2
var SAVE_PATH: String = "res://areas/rooms.cfg"
var config: ConfigFile = ConfigFile.new()
var current_room: PackedScene
func _on_area_entered(area: Area2D) -> void:
change_room(self)
func change_room(door: Door) -> void:
var room_spawner: Node2D = get_parent().get_parent().get_parent()
var player: CharacterBody2D = get_tree().get_root().get_node("Main/GameContainer/Game/Game/Player")
var transition: AnimationPlayer = get_parent().get_parent().get_parent().find_child("RoomTransition").find_child("AnimationPlayer")
var main: Node2D = get_tree().get_root().get_node("Main")
var last_room: Node2D = get_parent().get_parent()
door.current_room = door.next_room
transition.stop()
transition.play("transition")
player.cutscene = true
player.velocity.x += 20 * player.direction
main.cutscene = "transition"
await get_tree().create_timer(0.7).timeout
-> var room = current_room.instantiate() # The error is here!
room_spawner.add_child(room)
player.global_position = door.player_position
player.velocity.x = 0
await get_tree().create_timer(0.5).timeout
player.cutscene = false
last_room.queue_free()
I think i get your error. If you try to declare the chnage_room value which is a PackedScene in the _ready() function. Your error is solved. By doing this it ensures that the change_room has setted it’s value before it instantiate it.
ERROR DESCRIPTION
The error is why you load the door it somehow does not have value in the change_room. And when you try to make instance of change_room an error occured.
I know what _ready() is, I just didn’t knew what you meant with change_room = value.
Do you mean current_room = preload("something")
That wouldn’t fix the issue though, because every door node has it’s next_room atached. in door1 i chose Room_1_1.tscn, on door2 i chose Room_2_1.tscn next_room is an export variable wich lets me change its value in the inspector. then current_room will have the next_room value, then current_room will be instantiated and the room will be replaced by the current_room. but as you saw, because the 2 doors don’t have the same value. the error apears when entering door2
assign the next_room value to current_room in _ready() function. Becuase the error you face says change_room do not have any value when you try to instanciate the object. Like this var room = current_room.instantiate()
I DISCOVERED THE BUG! THERE WAS NOTHING WRONG WITH THE CODE! Room_3_1 had 2 door nodes, i delted them and I was able to go to Room_3_1 without errors, how?
it means the third door does not have any scene setted. Thats your actual problem.
to fix this you have to assign the third scene but if you have none to assign then try following code.
the below code only set the room when it has any value.
if current_room :
var room = current_room.instantiate()
room_spawner.add_child(room)