Too Many CharacterBody2D Collisions Leads to Null Instance Error

Godot Version

Godot 4.3-Stable

Question

I am making a Bullet Hell type of game, and so far have the player character and enemy dots that are “heat seeking” missiles. When the enemy dots make contact with the player, the player takes damage and the enemy despawns. I have this working with the following code

In the Player’s Script:

extends CharacterBody2D

func _physics_process(delta):

    for i in get_slide_collision_count():
	    var collision = get_slide_collision(i)
	    if move_and_slide():
		    print(collision.get_collider())
		    Signals.delete_enemy.emit(collision.get_collider())
		    health -= 15

Everytime the player collides with something, it sends the RID to a script called “Signals” which is Autoloaded

In the Signals Script:

extends Node

signal delete_enemy(delete_enemy: RID)

The Enemy Dot script checks the RID in Signals (aka the RID of whatever collided with the player) and if that RID is the same as its own then it deletes itself

In the Enemy Dot’s Script

extends CharacterBody2D

func _ready():
    Signals.delete_enemy.connect(_on_delete_enemy)

func _on_delete_enemy(delete: RID):
	deleteRID = delete

func _physics_process(delta):
	
	if deleteRID == get_rid():
		queue_free()

I’m aware this probably isn’t the best code to perform this, it’s my third day using Godot, but it works — Except that when too many Enemy Dots collide with the player at once the game stops with the following error:

Attempt to call function 'get_collider' in base 'null instance' on a null instance.

If anyone could help me solve this issue, or educate me on a better way to do what I described, help would be appreciated. Thank you!

You can check is_instance_valid() before you run the code.