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. -
Use
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
GetActorX,GetActorY, andVectorLength. If that distance is closer thanuser_closest_target(oruser_closest_targetis -1, meaning this is the first candidate checked), updateuser_closest_targetand set the source'stargetto this candidate. -
Back in the inventory item, immediately
stopso the item is discarded and doesn't junk up a bunch of inventories. -
Back in the source, immediately after the
A_RadiusGive, do whatever you like with your newtarget. (Note that it might be null, if there were no candidates at all.)
All of this happens synchronously within a single tic, so if you really need to, you can safely juggle TIDs as long as you set them back afterwards.