# Which will be the more efficent AI

**URL:** <https://forums.unrealengine.com/t/which-will-be-the-more-efficent-ai/39315>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [August 22, 2015, 1:58am UTC](https://forums.unrealengine.com/t/which-will-be-the-more-efficent-ai/39315 "2015-08-22T01:58:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [August 22, 2015, 1:58am UTC](https://forums.unrealengine.com/t/which-will-be-the-more-efficent-ai/39315/1 "2015-08-22T01:58:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [August 22, 2015, 9:25am UTC](https://forums.unrealengine.com/t/which-will-be-the-more-efficent-ai/39315/2 "2015-08-22T09:25:18Z")

</div>

> [@envenger;361391](#):
>
> I don’t want to store the player character in a variable and detect if the player is near the AI through a decorator.

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.

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [August 22, 2015, 1:26pm UTC](https://forums.unrealengine.com/t/which-will-be-the-more-efficent-ai/39315/3 "2015-08-22T13:26:43Z")

</div>

+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.

---

<div class="post-metadata">

**Author:** ![anon26705860](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon26705860](https://forums.unrealengine.com/u/anon26705860)\
**Post date:** [August 22, 2015, 5:06pm UTC](https://forums.unrealengine.com/t/which-will-be-the-more-efficent-ai/39315/4 "2015-08-22T17:06:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [August 23, 2015, 4:12am UTC](https://forums.unrealengine.com/t/which-will-be-the-more-efficent-ai/39315/5 "2015-08-23T04:12:13Z")

</div>

> [@kamrann;361542](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [August 23, 2015, 7:16am UTC](https://forums.unrealengine.com/t/which-will-be-the-more-efficent-ai/39315/6 "2015-08-23T07:16:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [August 23, 2015, 9:54am UTC](https://forums.unrealengine.com/t/which-will-be-the-more-efficent-ai/39315/7 "2015-08-23T09:54:16Z")

</div>

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:

```auto

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:

```auto

SearchRadiusSqr = SearchRadius * SearchRadius;

```
