Hey there, I'm working on my own little Vampire Survivors style top down bullet heaven game to teach myself GDScript.
I have an enemy spawner that runs on a timer and generates new instances of a handful of different preloaded enemy scenes, adds them as child nodes to itself and also adds them to a group called "enemies".
The enemies themselves dont really do anything besides moving towards the player and colliding with each other, however as soon as there are 200+ enemies all bunched up and colliding with other enemies around them the performance suddenly drops from a steady 144FPS to just 5. The Profiler shows that the main culprit seems to be the physics_process function in the enemy scene.

func _physics_process(delta):
	
	var dir = (player.global_position - global_position).normalized()
	move_and_slide(dir * movementspeed * 100 * delta)
	
	if dir.x <= 0:
		$Sprite.set_flip_h(true)
	else:
		$Sprite.set_flip_h(false)

I tried making it so they only try to move if they are only colliding with less than or equal to X other entities, to reduce the load when all the enemies are bunched up and most of them don't move anyway, however, to have any impact on performance X needs to be smaller than 4 and at that point it just causes enemies to clump up and get stuck on one another forever, not moving.

func _physics_process(delta):
	
	var dir = (player.global_position - global_position).normalized()
	
	if self.get_slide_count() <= 4:
		move_and_slide(dir * movementspeed * 100 * delta)
	
	if dir.x <= 0:
		$Sprite.set_flip_h(true)
	else:
		$Sprite.set_flip_h(false)

I'm still very new to Godot (and programming in general, really) so I'm probably just handling enemy spawning or movement in a bad way altogether but I couldn't really find a solution by googling and browsing forums. Any help would be greatly appreciated.

PS: I hope the code formatting and inserting the screenshot of the profiler worked, not familiar with the forum either 😃

    StillProphet Edit: I've tried declaring the dir variable outside of the physics_process and just changing its value each tick and ive tried using move_and_collide instead, neither of which had any noticable impact on performance.