I needed a monster to set the nearest corpse as its
target, but the same idea could be adapted to anything else easily enough.
Clear the source's
target. Set some user var on the source,
user_closest_target, to a null-ish value like -1.
A_RadiusGivefrom the source to give a dummy inventory item to every candidate actor.
From the inventory item, call an ACS script.
In the ACS script (where the candidate is the activator), check the distance from the candidate to the source via
VectorLength. If that distance is closer than
user_closest_targetis -1, meaning this is the first candidate checked), update
user_closest_targetand set the source's
targetto this candidate.