Download

Which will be the more efficent AI

My game has around 150 instances of an AI at a given time.

The AI are very simple and roam around randomly and attack their target when it comes close.
I don’t want to store the player character in a variable and detect if the player is near the AI through a decorator.

Instead I could use a cylinder like shape and detect the player through on-overlap delegates to detect when the player is in range. I assume the AI perception component does exactly the same. But as I AI is relatively simple, I didn’t want to use the AI perception component.

So I wanted to know which of the 3 will be more efficient to work with.

  1. Checked the distance between AI and player through a service.
  2. Using overlap through the overlap of a shape.
  3. Using AI perception component.

Why not? Always go with the most obvious and straightforward solution until you have a particular reason for needing to change it. Or are you saying that this doesn’t seem like the simplest method to you? To me it is.

It’s not clear to me if you’re primarily concerned with performance (which you shouldn’t be) or something else. But if it is performance, a distance check is just a handful of subtractions, multiplications and additions. It’s way, way simpler than a 3D overlap test, even if the latter is optimized by PhysX.

+1 with Kamrann

Storing the variable is definitely the fastest way to do it. Using an overlap will be really slow since you’ll be querying the scene every frame for 150+ actors, which will all query each other etc… it’s not scaleable since your queries become n^2.

AI perception sight sense will first do a angle check, then a distance check and then a ray trace. If you want simplicity just do the service or decorator approach.

Thanks for the reply… I was not sure how much overlap tests costed for something as simple as a hexagon.

There will be cases where the AI might have ~20+ enemies to detect and attack.
I thought of something that might decrease the the cost.

Suppose the distance between between the enemy and bot is really high in the first check, it perform the next with a greater delay.

Yep, there are lots of tricks you could potentially use to optimize it, but from the numbers you’ve posted so far, doing a distance check here isn’t going to be anywhere close to being a performance bottleneck. I can’t stress enough that you shouldn’t even think about performance issues for something like this until you actually encounter them.

Also, try to use Distance Squared whenever doing those Distance checks, since it’s faster to ‘Square’ the search distance, than it is to perform a SquareRoot on the length of the Distance Vector. For example:



const float DistanceSqr = FVector(MyLocation - ItsLocation).SizeSquared();
if (DistanceSqr < SearchRadius * SearchRadius)
{
    // Do Stuff
}


It’s probably worth caching the SearchRadius Squared Value as well, since that makes it even faster! Means it can’t be a const though, since you’ll probably want to do this in PostInitializeComponents() which will then Square whatever value you set for SearchRadius:



SearchRadiusSqr = SearchRadius * SearchRadius;