I feel ashamed to admit I’ve never programmed RTS units, but I see a few things. First of all, _target() calls get_overlapping_bodies() even though you had already found those bodies in process. You could just send those from process to the target() function. Also remember get_parent() is still a function, so it’s probably better to save it and reuse the parent node var p = get_parent() than to just keep calling get_parent(). That should offer a speed boost.
Actually, to be honest, I don’t think this needs to be done in the process func. This is how I’d do it for performance:
RTS units have a line of sight, right? Well I’m sure they also have a separate radius in which they’ll aggro an enemy, but for this lets just assume it’s the same. All you need is to give the unit his/her area2d, then attach the body_entered() signal to script.
Something like this:
var target : UnitClass
func body_entered(body):
if body is Unit:
If body.distanceToSelf() < target.distanceToSelf() and self.notBusy:
If body.allied:
Print(“what’s up, bro!”)
else:
print(“grrr”)
target = body
# react to target in other function presumably.
If the enemy enters the unit’s LOS, do stuff, else chillax. In process I’d have it so that once per frame the unit checks for closer threats or whatever other situations to look for.