Godot Version
4.7
Question
This script is attached to 2 different room summoning nodes, it is supposed to set up the area2d then delete the rest of the room until the player enters it again. the player is in the “RoomChangeCheck” group. the room the player spawns in works perfectly, but the other room’s area2d disappears after the player enters the room, successfully loading in the room, but not staying long enough for the player’s area2d to detect the room’s area2d. i need both of the area2ds to stay completely the same after they are resized.
@tool
extends Node2D
var ShouldDelete = 0
@export var starting_room : bool = false
var newroom = null
var borderset = false
var roomcreated = false
#Makes rooms visible in editor
@export var room : PackedScene:
set(value):
if room != value:
room = value
if Engine.is_editor_hint():
_create_room()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
#creates only the room the player is in
if not Engine.is_editor_hint():
ShouldDelete = 1
_create_room()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
if Input.is_action_just_pressed("ui_down"):
_reset_rooms()
#creates room and sets zone detector to the right size, then deletes the room if the player is not in it.
func _create_room():
#creates room
newroom = room.instantiate()
add_child(newroom)
#sets zone detector to the correct size
if borderset == false:
$Area2D.scale.x = newroom.sizeX
$Area2D.scale.y = newroom.sizeY
$Area2D.position.x = (newroom.sizeX * 16)
$Area2D.position.y = (newroom.sizeY * 16)
borderset = true
#deletes zone if the player is not in it
if ShouldDelete == 1:
ShouldDelete = 0
newroom.queue_free()
newroom = null
roomcreated = false
#creates room if the player enters it
func _on_area_2d_area_entered(area: Area2D) -> void:
if area.is_in_group("RoomChangeCheck"):
if roomcreated == false:
roomcreated = true
ShouldDelete = 0
_create_room()
func _reset_rooms():
var IsInZone : bool = false
for i in $Area2D.get_overlapping_areas():
if i.is_in_group("RoomChangeCheck"):
IsInZone = true
break
if newroom is Node2D and not IsInZone == true:
newroom.queue_free()
newroom = null
roomcreated = false