**Two New AI Nodes
Get Closest Actor Of Class In Radius of Location
Get Closest Actor of Class In Radius of Actor**
These nodes are great for use with AI calculations!
**C++ Code For You**
Here's my c++ code for **Get Closest Actor of Class In Radius of Actor**!
```
AActor* UVictoryBPFunctionLibrary::GetClosestActorOfClassInRadiusOfActor(
UObject* WorldContextObject,
TSubclassOf<AActor> ActorClass,
AActor* ActorCenter,
float Radius,
bool& IsValid
){
IsValid = false;
if(!ActorCenter)
{
return nullptr;
}
const FVector Center = ActorCenter->GetActorLocation();
//using a context to get the world!
UWorld* const World = GEngine->GetWorldFromContextObject(WorldContextObject);
if(!World) return nullptr;
//~~~~~~~~~~~
AActor* ClosestActor = nullptr;
float MinDistanceSq = Radius*Radius; // Radius
for (TActorIterator<AActor> Itr(World, ActorClass); Itr; ++Itr)
{
//Skip ActorCenter!
if(*Itr == ActorCenter) continue;
//~~~~~~~~~~~~~~~~~
const float DistanceSquared = FVector::DistSquared(Center, Itr->GetActorLocation());
//Is the closest possible actor within the radius?
if (DistanceSquared < MinDistanceSq)
{
ClosestActor = *Itr; //New Output!
MinDistanceSq = DistanceSquared; //New Min!
}
}
IsValid = true;
return ClosestActor;
}
```
Download Link (6.5mb)
UE4 Wiki, Plugin Download Page
Enjoy!