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.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
Jarewill
 
 
Posts: 1768
Joined: Sun Jul 21, 2019 8:54 am

Scan for nearby enemies in ZScript

Post by Jarewill »

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.
User avatar
Cherno
Posts: 1311
Joined: Tue Dec 06, 2016 11:25 am

Re: Scan for nearby enemies in ZScript

Post by Cherno »

Use a BlockThingsIterator.

Code: Select all

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.
Jarewill
 
 
Posts: 1768
Joined: Sun Jul 21, 2019 8:54 am

Re: Scan for nearby enemies in ZScript

Post by Jarewill »

Thank you Cherno!
This seems to be working how I wanted!
User avatar
Cherno
Posts: 1311
Joined: Tue Dec 06, 2016 11:25 am

Re: Scan for nearby enemies in ZScript

Post by Cherno »

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.
Post Reply

Return to “Scripting”