My Case
Briefly, in construction script I have some logic which uses Player Camera Manager, but when I call the same exact function but from inside c++ function i get an engine crash due to access violation. I did some debugging and found out that Player Controller is not yet instanced by the time I try to access it.
Blueprints
So when I call the pure function GetPlayerCameraManager (which implicitly calls UGameplayStatics::GetPlayerController) in Blueprints, everything works as intended.
I have something like this in my code (oversimplified):
- Construction Script
- The function that calls Get Player Camera Manager
The Problem
In the base class that my Blueprint is based on I added this function:
void ATestActor::CallGetPlayerCameraManager()
{
APlayerCameraManager* CameraManager = UGameplayStatics::GetPlayerCameraManager(GetWorld(), 0);
if (IsValid(GEngine))
GEngine->AddOnScreenDebugMessage(0, 3.f, FColor::Red, CameraManager->GetName());
}
Then when I try to call this function in construction script the engine crashes:
Most importantly, the debugger shows that the problem is that UGameplayStatics::GetPlayerController() (which is used in GetPlayerCameraManager()) returns nullptr. The question therefore is why this happens and how can I change my code for it to work