Scan for nearby enemies in ZScript

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Scan for nearby enemies in ZScript

Postby Jarewill » Mon Jun 29, 2020 10:38 am

Hello.

I'm looking for a way to "scan" for nearby enemies in ZScript.
What I mean is that I want to get all monsters within a certain radius and return pointers to them, so I can check their classes and states.
My current idea is to use LineTrace, but it will have troubles with height differences. (Scanning on stairs and such)
I was also looking in the GZDoom's ZScript files, to see how CheckRange did it, but I haven't been able to find anything aside a native function.

I'm still new to ZScript, so I wanted to ask here in case anyone knows how to do it.
Thank you.

While I am here, I wanted to ask something about LineTrace.
Is using many LineTraces going to impact performance a lot?
I haven't noticed anything yet, unlike when I was originally using LineAttack for checks, which spawned a lot of puffs.
Jarewill
 
 
 
Joined: 21 Jul 2019

Re: Scan for nearby enemies in ZScript

Postby Cherno » Mon Jun 29, 2020 10:47 am

Use a BlockThingsIterator.

Code: Select allExpand view
double maxDistance = 512;
for (let i = BlockThingsIterator.Create(self, maxDistance); i.Next();)
      {
         Actor other = i.thing;
         if (other == self)
         {
            continue;
         }
                         double distance = Distance3D(other);
                        if(distance > maxDistance)
         {
            continue;
         }
                        if(other.bISMONSTER == false)
         {
            continue;
         }
                        console.printf(String.Format("%s (%f)",other.GetClassName(),distance));
                }

Last edited by Cherno on Mon Jun 29, 2020 11:35 am, edited 2 times in total.
User avatar
Cherno
 
Joined: 06 Dec 2016

Re: Scan for nearby enemies in ZScript

Postby Jarewill » Mon Jun 29, 2020 11:33 am

Thank you Cherno!
This seems to be working how I wanted!
Jarewill
 
 
 
Joined: 21 Jul 2019

Re: Scan for nearby enemies in ZScript

Postby Cherno » Mon Jun 29, 2020 11:39 am

Note that you have to include the distance check, since the BlockThingsIterator's radius parameter is only used to get the blockmaps blocks to check within that radius; Actor that are farther away than this radius will also be found, so they need to be weeded out with a seperate check versus the actual distance between the calling actor (player, in this case) and the found actor.
User avatar
Cherno
 
Joined: 06 Dec 2016


Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests