Enemies respawn on window resize

Godot v 4.2.1

I’m working on a 2D dungeon crawler/ rouge like game and I experience the following problem. When I resize the window new enemies spawn (not intended).

I’m doing the spawning logic at a room level, having 4 Marker2D and “randomly” spawning an enemy at the location of the Marker2D.

I attached the script below and here’s a clip of this happening:

On a closer view it looks like the whole game resets when the room needs to resize…

extends Node2D
@onready var room = $"."

@onready var spawn_point_1 = $SpawnPoint1
@onready var spawn_point_2 = $SpawnPoint2
@onready var spawn_point_3 = $SpawnPoint3
@onready var spawn_point_4 = $SpawnPoint4

const donut = preload("res://Enemy/Donut/donut.tscn")
var SpawnPoints = []

func _ready():

func spawn_enemies():

	for i in SpawnPoints:
		if randi_range(1,5) > 2.5:
			var new_donut = donut.instantiate()
			new_donut.global_position = i.global_position

so i assume, they dont spawn that much if you dont resize?

who else can call this method?

1 Like

This is the only place where it’s called.

The Idea is to spawn the enemies when the room is initialized.

But as I observed later, also the position of the doors changes when I resize the window, which means that also the code that initializes the rooms get recalled (that one being also in the _ready() function).

So it looks to me that the _ready() functions gets called on window resize?

extends Node2D

var Room = preload("res://Rooms/room.tscn")
var player = preload("res://Player/player.tscn")
var loot_manager =preload("res://PowerUps/loot_manager.tscn")

var room_count = 5
var rooms = []

func _ready():
	generate(room_count - 1)
func add_player():
	await get_tree().create_timer(0.5).timeout

	var main = get_tree().current_scene
	player = player.instantiate()
	player.global_position = Vector2.ZERO
	PlayerStats.health = PlayerStats.max_health
	loot_manager = loot_manager.instantiate()
	loot_manager.global_position = Vector2.ZERO
func add_room(position):
	if rooms.has(position):
		return 0
	return 1 

func generate(count):
	rooms = [Vector2i(0, 0)]
	var from = rooms[-1]
	while count > 0:
		var dir = [Vector2i(-810,0), Vector2i(810,0), Vector2i(0,-490), Vector2i(0,490)].pick_random()
		var to = from + dir
		count -= add_room(to)
		from = to
func _draw():
	for r in rooms:
		var Room = Room.instantiate()
		var main = get_tree().current_scene
		Room.global_position = r
func reload_level():

func _input(event):
	if event.is_action_pressed("ui_accept"):

This is the bug, you don’t add stuff every time you draw, that’s not right, and when you resize it redraws

1 Like

So if I understand correctly, the _draw() function gets called when resizing the window?

If so, except for turning off resizing in project settings, how may one go about this issue?

You add them elsewhere, not in _draw, that’s just not right

1 Like

_draw is for drawing things, not adding nodes, just put the code in add_room or generate

1 Like

This works!

The _draw() function was left there by a code that I modified to serve as the basis for my procedural generation of rooms.

Thank you for taking the time! :slight_smile:

1 Like