Hello,
I’ve created a Behaviour Tree Task where I want to return the random location of an actor in the scene of a particular class. However, I can’t get the UGameplayStatics::GetAllActorsOfClass
function call to work.
This is my Header file
public:
explicit UMyBTTask_FindRandomLocation(FObjectInitializer const& ObjectInitializer);
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<AActor> ClassToFind;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TArray<AActor*> FoundActors;
This is the CPP file
EBTNodeResult::Type UMyBTTask_FindRandomLocation::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) {
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ClassToFind, FoundActors);
UE_LOG(LogTemp, Error, TEXT("Number of Actors %f"), FoundActors.Num());
return EBTNodeResult::Failed;
}
When I print out the length of the array it’s always 0. I’ve tried implementing the code for this function manually and it does successfully iterate through all of the actors that belong to the class that I am after, but adding those actors to the array just doesn’t seem to work.
for (TActorIterator<AActor> It(GetWorld(), ClassToFind); It; ++It)
{
AActor* CurrentActor = *It;
UE_LOG(LogTemp, Error, TEXT("Actor name %s"), *FString(CurrentActor->GetName()));
FoundActors.Add(CurrentActor);
}
I thought perhaps arrays just aren’t working for me? So I tried running the following code which also has a length of 0, so I am beyond confused
TArray<FString> ArrayString;
ArrayString.Add(TEXT("dasdasdas"));
UE_LOG(LogTemp, Error, TEXT("Number of strings %f"), ArrayString.Num());
I am on Unreal Engine version 5.3.2