UE 4.9 Controller::GetPawn() throw Null exception

This is not really a bug (in my opinon) but this is a behvaior which I didn’t get in Unreal Engine 4.8.3
and I don’t realy know why this happend in Unreal Engine 4.9.

Problem:
I override the GetControlRotation from my AAIController (everything I do, was done in c++ expect the content binding).
In this function I need to get the reference from the controlled Pawn (GetPawn()) but when I start to run the game in the Editor I get a null reference from GetPawn() in the GetControlRotation() function but only in the first call of this function.

Solution:
obviously checking the GetPawn() for nullptr.

My question is, why this happend in 4.9? I the release Note and couldn’t find any reason for this behavior.
Or did I miss understand something?

I use the released version not the GitHub version and I find the problem by debugging the project via VS2013.
The Projects template is the ThirdPerson-Template C++ version.

I could reproduce this behavior by writing a Controller which override the GetControlRotation function with the following code

FRotator AMyAIController::GetControlRotation() const
{
	GetPawn()->GetActorClass(); // call some function from 
							// GetPawn to throw a nullexecption
	return ControlRotation;
}

Hey

I was able to reproduce the null pointer in 4.9 and have entered a report (UE-20724) for investigation. As you mentioned the best solution for now is to check that the result of GetPawn() is valid before using it.

Cheers

Thanks, for the fast answer :slight_smile:

As says, it is always a good idea to verify results from functions like this, it is valid for a controller not to have a pawn. Is this problem only for one frame or so - ie is it possibly just an order-of-operation change between 4.8 and 4.9, and it worked correctly once the pawn and controller exist?

I debugged the project once again and the controller function was called for the Pawn constructor.

The reason why I (most of the time) didn’t check the GetPawn() function is because I thought without a Pawn a Controller couldn’t exist.
I allways create a Controller via this code in the Pawn constructor

AIControllerClass = AMyAIController::StaticClass();

How does the engine know which Controller to instatiate, for this pawn, without reading this Code? (I know this will be when the engine starts but I could change it via BP in a BP subclass)