How do I make target more than one thing

Godot Version

4

Question

I have this cannon that rotates towards an enemy but when that enemy dies the game crashes because there is nothing to target and its being asked to target something that's not there, how do I fix this or how would you code it? I would like it to switch targets to other enemy once that one is dead or out of range and to focus on the closest target, is there a class system I can use so the cannon would know what to aim at even if it's not the same enemy, I lack code as I don't know how to code such a thing with these problems in place

Your cannon should probably handle an idle state where there is no target at all, so it doesn’t crash.

Check in the process function if there is a target to point at (e.g. with getting the overlapping bodies of a area2d), and only if there is, the cannon should rotate towards it.

I don’t know how the get_overlapping_bodys works