Problem when changing maps

Godot Version

v4.2.2.stable.official

Question

I have this problem that if I go from any map to the Lake map, it will loop indefinitely between either Woods or Town to Lake really fast. While looped, I can see the Lake map blinking in and out for a split second.
Can anyone tell me where’s the problem at?

Here’s the project if needed

hierarchy2

Player Script:

class_name Player extends CharacterBody2D

@onready var sprite: Sprite2D = $Sprite2D

func _ready() -> void:
	MapManager.OnTriggerPlayerSpawn.connect(Spawn)

func Spawn(_position: Vector2, direction: String):
	global_position = _position

Doors script (shared on every door):

class_name Door extends Area2D

@export var mapDestinationTag: String
@export var doorDestinationTag: String
@export var spawnDirection = "up"
@onready var spawn = $Spawn

func _on_body_entered(body: Node2D) -> void:
	
	if body is Player:
		MapManager.GoToLevel(mapDestinationTag, doorDestinationTag)
		get_parent().get_parent().queue_free()

Maps script (shared on every map):

extends Node2D

func _ready() -> void:
	if MapManager.spawnDoorTag != null:
		_on_level_spawn(MapManager.spawnDoorTag)
		
func _on_level_spawn(destinationTag: String):
	var doorPath = "Doors/Door_" + destinationTag
	var door = get_node(doorPath) as Door
	#MapManager.TriggerPlayerSpawn(door.spawn.global_position, door.spawnDirection)
	call_deferred("spawn_player", door.spawn.global_position, door.spawnDirection)

func spawn_player(position: Vector2, direction: String) -> void:
	MapManager.TriggerPlayerSpawn(position, direction)

Map manager autoload script:

extends Node

var TOWN = load("res://ModularDoorMaps/town.tscn")
var WOODS = load("res://ModularDoorMaps/woods.tscn")
var LAKE = load("res://ModularDoorMaps/lake.tscn")

var spawnDoorTag
var mapNode

signal OnTriggerPlayerSpawn

func _ready() -> void:
	mapNode = get_node("../Game World").get_child(0)

func GoToLevel(levelTag, destiantionTag):
	var sceneToLoad
	match levelTag:
		"Town":
			sceneToLoad = TOWN
		"Woods":
			sceneToLoad = WOODS
		"Lake":
			sceneToLoad = LAKE

	if sceneToLoad != null:
		spawnDoorTag = destiantionTag
		var newMap = sceneToLoad.instantiate()
		mapNode.add_child(newMap)

func TriggerPlayerSpawn(_position: Vector2, direction: String):
	OnTriggerPlayerSpawn.emit(_position, direction)

When entering a door does the player spawn within a door area? Try moving the player away from the door if so.

Adding a delay before doors are operable may help too

class_name Door extends Area2D

@export var mapDestinationTag: String
@export var doorDestinationTag: String
@export var spawnDirection = "up"
@onready var spawn = $Spawn

func _ready() -> void:
	# half second before door works 
	await get_tree().create_timer(0.5).timeout
	body_entered.connect(_on_body_entered)

# remove in-editor connection
func _on_body_entered(body: Node2D) -> void:
	
	if body is Player:
		MapManager.GoToLevel(mapDestinationTag, doorDestinationTag)
		get_parent().get_parent().queue_free()
1 Like

Thanks, you’re right.
I forgot to move the spawn area away from the collider.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.