Multiple initialization of code

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!

How do you call _on_up_entrance_body_entered?

If that function is called twice, you could check the body argument.

To investigate further it would be great to know more about your scenes and how they’re composed

Hey sir, thanks for the reply,

_on_up_entrance_body_entered

is signal ‘body_entered’

additionally, if i log

func _change_room(body, direction: Global.ENTRY_POINTS):
	print(self)

it shows 2 logs, with different nodes…

this is my room scene, second script is loaded as autoload script

image

You’re welcome!

I made a simple test with an Area2D and an entering body. That works fine but I remember having the same symptom.

You could print some more info on what is happening:

func _change_room(body, direction: Global.ENTRY_POINTS):
	print("%s entered %s from %s" % [body, self, direction])

Checking whether everything is setup at runtime could also be a start. Have a look at the Remotes tab in the scene view to check if all nodes are where they belong.

When you still can’t figure out we need to look at how you’re managing the rooms.

Logs when character enter doors:

Fighter:<CharacterBody2D#38084281793> entered Room:<Node#38453380585> from 1
Fighter:<CharacterBody2D#38084281793> entered Room:<Node#38889588227> from 1

My main scene for some more scope:
image

My idea is to keep track of game state in singleton, i generate room matrix when game is starting, and i keep track of current coords of user.

Whenever user is leaving room:

  • i pass in which way should i update user coords.
  • i am getting information what room should be loaded
  • i remove old room (not sure if its correct yet, but its not the issue here)
  • i am adding new room
    (code is linked in previous comments)

It seems to me that everything is logically OK, but it is what it is. :slight_smile: