Download

Problem with TActorIterator<AnyType> In Custom "MyPlayerController.cpp"

I recently upgraded to 4.18 from 4.15. I have now encountered a problem with the TActorIterator, only inside AMyPlayerController it seems.

The error I get looks like this:


void AMyPlayerController::BeginPlayingState() {
      Super::BeginPlayingState();

       AMyTank* NewTank = Cast<AMyTank>(GetPawn());
      for (TActorIterator<AMyTank> TankIt(GetWorld()); TankIt; ++TankIt) {
                //Body
   }
 } 

There is an error when I try to do TActorIterator with the following error log:


2>c:\program files\epic games\ue_4.18\engine\source\runtime\engine\public\EngineUtils.h(490): error C2664: 'TActorIteratorBase<TActorIterator<AMyTank>>::TActorIteratorBase(TActorIteratorBase<TActorIterator<AMyTank>> &&)': cannot convert argument 2 from 'TSubclassOf<AMyTank>' to 'TSubclassOf<AActor>'
2>  c:\program files\epic games\ue_4.18\engine\source\runtime\engine\public\EngineUtils.h(490): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
2>  c:\program files\epic games\ue_4.18\engine\source\runtime\engine\public\EngineUtils.h(488): note: while compiling class template member function 'TActorIterator<AMyTank>::TActorIterator(UWorld *,TSubclassOf<AMyTank>,EActorIteratorFlags)'
2>  C:\..\..\Player\MyPlayerController.cpp(426): note: see reference to function template instantiation 'TActorIterator<AMyTank>::TActorIterator(UWorld *,TSubclassOf<AMyTank>,EActorIteratorFlags)' being compiled
2>  C:\..\..\\MainFiles\Source\PC\Player\MyPlayerController.cpp(426): note: see reference to class template instantiation 'TActorIterator<AMyTank>' being compiled

I did this same function in another class, in a class that extends UMG Widget, and it worked fine as of now - sometimes it didn’t work either, but it seems to consistently work now. But, now the error happens in this custom Player Controller class. If anyone has any information about this, I am all ears.

Just a wild guess, due to the engine change the order of includes have changed in your build. Do you include the AMyTank header file in the AMyPlayerController?

I didn’t originally. I tried adding it in the header file, and I still receive the same error. Strange enough, I received this error in my other class - in the widget - that uses this. I was able to fix it there by including AMyTank in the header, but not in AMyPlayerController.

EDIT: It only works if I do TActorIterator<AActor> in AMyPlayerController. Could this be a bug?

Does the same happens with TObjectIterator<AMyTank> ?!

TObjectIterator<AMyTank> works but is not what I want. It seems to go through all instances of Tank created throughout the entire instance of the game - from what I understand. I want to iterate through the current actors though, not objects that have been made in the past.

It just seems to not work in the PlayerController for some reason.

Maybe someone did ‘optimizations’ to the function and broke usability, happens all the time.
People touching this area of the engine doesn’t read the forums so you’d have to file a bug report on AnswerHub + GitHub and hope anybody notices.

Can you show us the declaration of the AMyTank class, just to be sure.

If anyone else runs into this issue, the problem for me was there was already a


#include "Engine/World.h"

, which forward-declares TActorItr but doesn’t fully declare it. You have to


#include "EngineUtils.h"

to get the full declaration.