Skip to content

Instantly share code, notes, and snippets.

@eevee
Created December 15, 2016 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eevee/a086846f4a1510e0a56a99b0b15e74cf to your computer and use it in GitHub Desktop.
Save eevee/a086846f4a1510e0a56a99b0b15e74cf to your computer and use it in GitHub Desktop.
Find the nearest/furthest actor from a source in ZDoom DECORATE

I needed a monster to set the nearest corpse as its target, but the same idea could be adapted to anything else easily enough.

  1. Clear the source's target. Set some user var on the source, user_closest_target, to a null-ish value like -1.

  2. Use A_RadiusGive from the source to give a dummy inventory item to every candidate actor.

  3. From the inventory item, call an ACS script.

  4. In the ACS script (where the candidate is the activator), check the distance from the candidate to the source via GetActorX, GetActorY, and VectorLength. If that distance is closer than user_closest_target (or user_closest_target is -1, meaning this is the first candidate checked), update user_closest_target and set the source's target to this candidate.

  5. Back in the inventory item, immediately stop so the item is discarded and doesn't junk up a bunch of inventories.

  6. Back in the source, immediately after the A_RadiusGive, do whatever you like with your new target. (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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment