Initializing a PlayerController in my character's constructor crashes the editor

I know there are no syntax errors here, so i must be breaking some rule.

I have this:

APlayerController* PController = Cast<APlayerController>(GetWorld()->GetFirstPlayerController());

in my constructor for a default pawn. For some reason the client crashes every time I run this. What am I doing wrong?

Hey Snowl0l-

The reason the crash is occurring is the call to GetWorld(). It is attempting to get the game world which doesn’t exist when the game isn’t running. If you move that line of code to BeginPlay() instead, you can avoid the crash. Another good idea is to check for the game world prior to making the call. If you add if(GetWorld()) before your line of code, it will first check that the world exists. If it does not, then the cast will be ignored until the construction script is called again.

Cheers

Doug Wilson