Download

Can't access custom Player Controller class

Hi everyone,

I’m trying to access a custom Player Controller class through a character, but every time I compile, the engine crashes and points to this line. I’m a bit of a newbie to Unreal and C++ so I’ve looked around and found there are multiple ways to get the Player Controller, but all of them so far have resulted in the same. I have tried:



AMyPlayerController* MyController = Cast<AMyPlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));


and also



AMyPlayerController* MyController = Cast<AMyPlayerController>(GetWorld()->GetFirstPlayerController());


The reason I want to access the Player Controller is that I want to set in a variable there to trigger the visibility of a widget, e.g. if the player collides with an object, I can pass through a variable to trigger the widget.

Thanks for your time,

Alo_X

You should be checking that pointers are valid before using them.

I would rewrite it like this:



auto pWorld = GetWorld();
if( pWorld )
{
     AMyPlayerController* MyController = Cast<AMyPlayerController>(pWorld->GetFirstPlayerController());
     if( MyController )
     {
     // Do stuff here
     }
}


HTH

Thank you ryan20fun for your reply, your solution worked! :smiley:

Your welcome :slight_smile:

I just remembered something I left out.

I recommend logging errors/warnings/etc to the log so that you can find out what went wrong.

You can add a “else” statment to the checks like this:



else
{
     FString msg( TEXT( "Failed to do X" ) );
     UE_LOG( LogTemp, Error, TEXT( "%s" ), *msg );
}


I have created these global functions to handle this for me:



void LogInfo( const FString& Function, const int LineNumber, const FString& Message, const bool SendToLog,
				   const bool AddToScreen, const float Duration )
	{
		if ( SendToLog || AddToScreen )
		{
			FString msg( TEXT( "" ) );// ( TEXT( "[ServerSetEnergyCell_Implementation] Cell parameter is null!" ) );
			msg.Append( Function );
			msg.Append( TEXT( "] Line " ) );
			msg.AppendInt( LineNumber );
			msg.Append( TEXT( ", Msg: " ) );
			msg.Append( Message );

			if ( SendToLog )
				UE_LOG( LogTemp, Log, TEXT( "%s" ), *msg );

			if ( AddToScreen && GEngine )
				GEngine->AddOnScreenDebugMessage( INDEX_NONE, Duration, FColor::Green, msg );
		}
	}
	void LogError( const FString& Function, const int LineNumber, const FString& Message, const bool SendToLog,
				   const bool AddToScreen, const float Duration )
	{
		if ( SendToLog || AddToScreen )
		{
			FString msg( TEXT( "" ) );// ( TEXT( "[ServerSetEnergyCell_Implementation] Cell parameter is null!" ) );
			msg.Append( Function );
			msg.Append( TEXT( "] Line " ) );
			msg.AppendInt( LineNumber );
			msg.Append( TEXT( ", Msg: " ) );
			msg.Append( Message );

			if ( SendToLog )
				UE_LOG( LogTemp, Error, TEXT( "%s" ), *msg );

			if ( AddToScreen && GEngine )
				GEngine->AddOnScreenDebugMessage( INDEX_NONE, Duration, FColor::Red, msg );
		}
	}
	void LogWarning( const FString& Function, const int LineNumber, const FString& Message, const bool SendToLog,
				   const bool AddToScreen, const float Duration )
	{
		if ( SendToLog || AddToScreen )
		{
			FString msg( TEXT( "" ) );// ( TEXT( "[ServerSetEnergyCell_Implementation] Cell parameter is null!" ) );
			msg.Append( Function );
			msg.Append( TEXT( "] Line " ) );
			msg.AppendInt( LineNumber );
			msg.Append( TEXT( ", Msg: " ) );
			msg.Append( Message );

			if ( SendToLog )
				UE_LOG( LogTemp, Warning, TEXT( "%s" ), *msg );

			if ( AddToScreen && GEngine )
				GEngine->AddOnScreenDebugMessage( INDEX_NONE, Duration, FColor::Yellow, msg );
		}
	}