Godot Version
4.2.2
Question
Hello everyone, I’m new to Godot and I’ve encountered a problem that I’m currently unable to understand. Could someone please help me figure out why my code is executing twice with the current solution?
Some details:
I am creating map generator, which is based on data matrix and room layout. So it looks like this
[0, 3, 2, 0]
[0, 0, 2, 0]
[0, 0, 1, 0]
The player respawns at 1. Each room has an entrance, and when the player enters, it should change the scene to another randomly generated room. The code looks like this:
level_manager.gd
var room_scene = preload("res://scenes/world/room/room.tscn")
func game_start():
# some other logic
_start_room()
func change_room(direction: Global.ENTRY_POINTS):
entry_point = direction
Global.room_node.call_deferred("remove_child", room_scenes_matrix[current_room.y][current_room.x])
current_room = get_new_room_coords()
call_deferred("_start_room")
func _start_room():
var room_to_start = room_scenes_matrix[current_room.y][current_room.x]
if (room_to_start == level_generator.ROOM_TYPES.ROOM):
room_scene.instantiate()
Global.room_node.add_child(room_to_start)
return
# other logic..
room.gd
func _ready():
_disable_not_available_entrances()
_move_character_to_proper_place()
pass
func _change_room(body, direction: Global.ENTRY_POINTS):
if (body.name == "MainCharacter" and already_entered == false):
level_manager.change_room(direction)
already_entered = true
func _on_up_entrance_body_entered(body):
_change_room(body, Global.ENTRY_POINTS.UP)
# etc...
The change_room function is being triggered twice, and the room instance’s _ready function is also called twice. I added multiple debug logs, but still doesnt get it.
Character after entering new scene is not touching “entrance” so its not called immediately.
I understand that I might be managing the room instances incorrectly, but I’m out of ideas on how to fix this. Any advice would be greatly appreciated!