Hi,
I’m having trouble with pointers (I think) when iterating over a TArray and trying to grab the results. I have a main actor with an overlapping sphere collider that detects other actors within it’s radius. It puts these overlapping actors into an array (a TArray). Then I iterate over the array to try to find the closest actor for the “main actor” to pick-up, via distance. The problem I’m having is that the actor my main character grabs is ALWAYS the same actor, every time, and it’s never a different actor, even if other actors happen to be closer in distance.
My theory on why my function is always grabbing the exact same actor (despite it being far away), is because there is a pointer always pointing to the same value. But I don’t know how to force the variable to be a different value, other than what it was first “pointed to” to begin with. I come from a C# background, and so pointers are very new to me. I think I understand them, but maybe I’m using them incorrectly in this case.
Here is my function that creates an array and “grabs” the closest actor that I want, and puts that actor/value into a function on another script (via parameter):
void UGameAnimInstance1::ScanEnemyToGrab()
{
mainCharacterActor = GetWorld()->GetFirstPlayerController()->GetPawn();
TArray<AActor*> EnemiesInSphereRadius;
TSubclassOf < AMyMedWarCharacterClass1 > ClassFilter;
// Clear the array
EnemiesInSphereRadius.Empty();
if (EnemiesInSphereRadius.Num() > 0)
{
EnemiesInSphereRadius.RemoveAt(0);
}
// Generate/populate the "EnemiesInSphereRadius" array with the resulting actors that are overlapped
mainCharacterActor->GetOverlappingActors(EnemiesInSphereRadius, ClassFilter);
int32 TempInt1 = EnemiesInSphereRadius.Num();
UE_LOG(LogTemp, Warning, TEXT("%d actors in the array"), TempInt1);
if (EnemiesInSphereRadius.Num() > 0)
{
// Use the first element (actor element) in the array
UMainCharacterController1 *ScriptComponent1 = mainCharacterActor->FindComponentByClass<UMainCharacterController1>();
// Set the "ActorToGrab1" variable to null
//ScriptComponent1->ActorToGrab1 = nullptr;
//ScriptComponent1->ActorToGrab1 = EnemiesInSphereRadius[0];
//AActor TempActor1 = EnemiesInSphereRadius[0];
//ScriptComponent1->AttachActorToFootSocket1(EnemiesInSphereRadius[0]);
// Set the initial distance float
InitialDistance1 = FMath::RoundToPositiveInfinity(900000000.0f);
for (int32 ActorIndex = 0; ActorIndex < EnemiesInSphereRadius.Num(); ActorIndex++)
{
ClosestDistance1 = (mainCharacterActor->GetActorLocation() - EnemiesInSphereRadius[ActorIndex]->GetActorLocation()).Size();
// Get the closest actor by distance to the main character in the array.
// For now though, I'm simply going to grab the first actor in the array if there is one.
if (ClosestDistance1 < InitialDistance1)
{
InitialDistance1 = ClosestDistance1;
TempActor1 = EnemiesInSphereRadius[ActorIndex];
//if (TempActor2 != nullptr)
//{
//ScriptComponent1->AttachActorToFootSocket1(EnemiesInSphereRadius[ActorIndex]);
//}
}
}
if (TempActor1 != nullptr)
{
ScriptComponent1->AttachActorToFootSocket1(TempActor1);
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("There are no enemies in this area!"));
}
//UE_LOG(LogTemp, Warning, TEXT("I ran the grab event!"));
}
I tried clearing the array every time the function is run, in order to ensure the array generated is populated with different actors, but it still grabs/returns the same actor every time.
Thanks for any help or info.