Game Crashing when more than 10 enemies

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Abb1x

My game is crashing when I kill the last enemy I think that’s beacuse there are too much enemies , How can I avoid crashes like this ? here is my spawner code :

extends Node2D

onready var screen_size = get_viewport().get_visible_rect().size
var spawnLocs = Array()
var minDist = 200# minimum distance between spawned sprites...
var rand = RandomNumberGenerator.new()

func _ready():
	get_object()
func _process(delta):
	var enemyscene = load("res://Scenes/VirusBlue.tscn")
	var enemy = enemyscene.instance()
	var obj = get_tree().get_nodes_in_group("Viruses")
	print(obj.size())
	restart_object()
func getNextSpawnLoc():
	while true:
		var x = rand.randf_range(0, screen_size.x)
		var y = rand.randf_range(0, screen_size.y)
		var newLoc = Vector2(x,y)
		var tooClose = false
		for loc in spawnLocs:
			if newLoc.distance_to(loc) < minDist:
				tooClose = true;
				break;
		if !tooClose:
			spawnLocs.append(newLoc)
			return newLoc
func restart_object():
	var obj = get_tree().get_nodes_in_group("Viruses")
	if obj.size() <= 5:
		get_object()
func get_object():
	rand.randomize()
	var enemyscene = load("res://Scenes/VirusBlue.tscn")
	var children = get_parent().get_children()
	for i in range (0,10):
		var nextLoc = getNextSpawnLoc()
		var enemy = enemyscene.instance()
		enemy.position = nextLoc
		add_child(enemy)

here is my enemy code :

   func _ready():
	add_to_group("Viruses")


func _physics_process(delta):
	if player == null:
		return

	var vec_to_player = player.global_position - global_position
	vec_to_player = vec_to_player.normalized()
	global_rotation = atan2(vec_to_player.y, vec_to_player.x)
	move_and_collide(vec_to_player * MOVE_SPEED * delta)
	
	if raycast.is_colliding():
		var coll = raycast.get_collider()
		if coll.name == "Player":
			coll.kill()

    func kill():
    	queue_free()
    func set_player(p):
    	player = p

Maybe not be related, but as a tip, move all your load scene such as:

var enemyscene = load("res://Scenes/VirusBlue.tscn")

to a preload statement (not inside process)

GameVisitor | 2020-04-05 15:42

:bust_in_silhouette: Reply From: supper_raptor

In this code you are instancing enemy every frame this will lead to memory leaks because they wont be freed by themselves.

func _process(delta):
    var enemyscene = load("res://Scenes/VirusBlue.tscn")
    var enemy = enemyscene.instance()
    var obj = get_tree().get_nodes_in_group("Viruses")
    print(obj.size())
    restart_object()

you are doing it wrong
1.) you are loading enemy scene every frame. (performance heavy)
2.) you are making instance of enemy and not using add_child or queue_free . (memory leak)

Then in this function , it may take lot of time depending on your luck to get position and it may result in hangs.

func getNextSpawnLoc():
    while true:
        var x = rand.randf_range(0, screen_size.x)
        var y = rand.randf_range(0, screen_size.y)
        var newLoc = Vector2(x,y)
        var tooClose = false
        for loc in spawnLocs:
            if newLoc.distance_to(loc) < minDist:
                tooClose = true;
                break;
        if !tooClose:
            spawnLocs.append(newLoc)
            return newLoc

I don’t understand your second point; I use Add_Child in my enemySpawner script what is the problem?

Abb1x | 2020-04-08 13:55