Hello everyone!
I've been working on a 2D game and I recently ran into some issues with my usage of Area2D nodes as doors. The way it is meant to work is that when the player enters the area and presses the interact button (E key) the door's script triggers and changes the scene to a different level, setting the player spawn in the process.
This works fine for the first level, where there's only one door, however, any level with two or more doors results in an error when trying to use any of them.
Error setting property global_position with value of type Nil
I've also noticed, after adding a message to be printed when using a door, which prints the name of the level it directs to along with its spawn point, that when entering a level with multiple doors or using a door in a level with multiple doors, all doors in the level print their messages, even though they're not supposed to do that unless interacted with.
I'm wondering if this is because the code for the doors is all based on a single script.
The code uses a Door_base script that is meant to handle the actual logic, with all other doors simply adding their respective spawns and level names:
extends Area2D
class_name Door_base
var LEVEL_NAME = ""
var SPAWN_POINT = ""
var CURR_SCENE = ""
func _ready():
connect("body_entered", self, "on_body_entered")
connect("body_exited", self, "on_body_exited")
func on_body_entered(body):
if(body.get("TYPE") == "PLAYER"):
#print("Player in!")
body.door = self
func on_body_exited(body):
if(body.get("TYPE") == "PLAYER"):
#print("Player out!")
body.door = null
func _on_open_door():
#print("door opened")
print("Level: ", LEVEL_NAME, " Spawn: ", SPAWN_POINT)
Globals.current_scene = CURR_SCENE
Globals.spawn_point = SPAWN_POINT
Globals.load_level(LEVEL_NAME)
This is an example of the code each door uses:
extends Door_base
func _ready():
LEVEL_NAME = "res://Scenes/Farms/Farm_south.tscn"
SPAWN_POINT = "Farm_south_spawn"
CURR_SCENE = "Farm south"
What am I doing wrong? Is there a way to get the current method to work or if there isn't, is there a better way to handle this kind of scene switching?
Thank you all for your help.