x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

GetAllActorsOfClass(): Cannot convert from TArray<> to TArray<> &

I am following the answers from here, but I still couldn't pass in the TArray into UGameplayStatics::GetAllActorsOfClass().

This is how my code looks like:

 #include "GetDebt.h"
 #include "../Player/Machine/BaseFrame.h"
 #include "FleeingObstacle.h"
 
 
 AFleeingObstacle::AFleeingObstacle(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer){
     //TTransArray<AActor*> Array = this->GetWorld()->PersistentLevel->Actors;
 }
 
 void AFleeingObstacle::BeginPlay(){
     Super::BeginPlay();
 
     //ERROR CODE
     TArray<ABaseFrame*> BaseFrameArray;
     UGameplayStatics::GetAllActorsOfClass(this->GetWorld(), ABaseFrame::StaticClass(), BaseFrameArray);
     if (BaseFrameArray.Num() > 0){
         LOG("Number of ABaseFrame in the TArray: " + FString::FromInt(BaseFrameArray.Num()));
     }
 }
 
 void AFleeingObstacle::Tick(float Delta){
     Super::Tick(Delta);
 }
 
 void AFleeingObstacle::Move(){
     LOG("MOVING... AT THE SPEED OF SOUND.")
 }

and the header file contains the following code:

 #pragma once
 
 #include "Obstacle/Obstacle.h"
 #include "FleeingObstacle.generated.h"
 
 /**
  * 
  */
 UCLASS()
 class GETDEBT_API AFleeingObstacle : public AObstacle
 {
     GENERATED_BODY()
 public:
     //PROPERTIES
     
 
     //FUNCTIONS
     AFleeingObstacle(const FObjectInitializer& ObjectInitializer);
 
     void BeginPlay() override;
 
     void Tick(float DeltaSeconds) override;
 
     void Move() override;
 };


I am still having this error popping up (Attachment provided):

alt text

What am I doing wrong this time? I am so confused.

Product Version: Not Selected
Tags:
ue4_error.png (6.1 kB)
more ▼

asked Apr 06 '15 at 01:58 AM in C++ Programming

avatar image

asperatology
293 31 30 91

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first

Hi asperatology.

The solucion is simple. UGameplayStatics::GetAllActorsOfClass isn't a template class so you must pass it an array of AActors*

Example Code:

 TArray<AActor*> FoundActors;
 UGameplayStatics::GetAllActorsOfClass(GetWorld(), AMyActor::StaticClass(), FoundActors);

 for (AMyActor* TActor: FoundActors)
 {
     AMyActor* MyActor = Cast<AMyActor*>(TActor);

     if(MyActor != nullptr)
             // Do what ever you need with your MyActor
 }
more ▼

answered Jan 21 '16 at 10:51 PM

avatar image

ja0335
51 2 8 9

avatar image asperatology Jan 21 '16 at 11:16 PM

Thanks. That sort of makes sense.

avatar image JethroRZ May 07 '19 at 09:02 AM

maybe a bit late, but just in case:

it should be

  for (AActor* TActor: FoundActors)
      {
          AMyActor* MyActor = Cast<AMyActor>(TActor);
      
          if(MyActor != nullptr)
                  // Do what ever you need with your MyActor
      }

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

I also understant what i do can use GetAllActorsOfClass

more ▼

answered May 11 '16 at 04:47 AM

avatar image

少狼_
0 1 3 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

UGameplayStatics::GetAllActorsOfClass will only accept TArray so your TArray BaseFrameArray; must be TArray BaseFrameArray;

Only after this then you can cast to whatever class you want.

more ▼

answered Apr 06 '15 at 06:43 AM

avatar image

Duncan Dam
3.1k 121 50 164

avatar image asperatology Apr 06 '15 at 09:18 PM

Thank you, but can you please rephrase your answer? I had to re-read your answer numerous times in order for me to understand that you meant:

 TArray<AActor*> BaseFrameArray;

And not:

 TArray<AActor> BaseFrameArray;

Nor:

 TArray<[INSERT SOME SUBCLASS OF AACTOR]> BaseFrameArray;

Nor:

 TArray<[SAME AS ABOVE] *> BaseFrameArray;

Nor:

 TArray BaseFrameArray;

Thank you again. :D

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question