Hello!
I’ve got a Player controller class, implemented using Enhanced Input system. It handles all inputs and then performs respective actions on possessed pawn. Originally developed for singleplayer I decided to test how it works in multiplayer. However, here is the problem. When Controller’s OnPossess method gets called I get assertion failure due to Input component being a nullptr (meaning that it has not been initialized yet).
void AMyPlayerController::OnPossess(APawn* aPawn)
{
Super::OnPossess(aPawn);
//Casting APawn we are provided with to AMyPlayer class and assigning it to PossessedPlayer variable.
PossessedPlayer = Cast<AMyPlayer>(aPawn);
checkf(PossessedPlayer, TEXT("The possessed Pawn is not of type (or derived from) AMyPlayer!"));
//Casting InputComponent (which actor provided us with) to UEnhancedInputComponent
EnhancedInputComponent = Cast<UEnhancedInputComponent>(InputComponent);
checkf(EnhancedInputComponent, TEXT("Failed to cast InputComponent to EnhancedInputComponent!")); //Here the assertion fails every time I try to launch multiplayer session!
//Get the local player subsystem
UEnhancedInputLocalPlayerSubsystem* InputSubsystem =
ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer());
checkf(InputSubsystem, TEXT("Unable to find Input subsystem!"));
//Wipe existing mappings for this Input Subsystem and insert ours
checkf(MappingContext, TEXT("No mapping context was provided!"));
InputSubsystem->ClearAllMappings();
InputSubsystem->AddMappingContext(MappingContext, 0);
//Binding the input actions
if(ActionLook)
EnhancedInputComponent->BindAction(ActionLook, ETriggerEvent::Triggered, this,
&AMyPlayerController::HandleLook);
//.....And bunch of other similar action bindings.....
if(UWorld* World = GetWorld())
{
APlayerCameraManager* PlayerCamera = World->GetFirstPlayerController()->PlayerCameraManager;
PossessedPlayer->CurrentCameraManager = PlayerCamera;
}
}
My assumption is that upon possessing, the Pawn is not fully initialized yet and therefore InputComponent is still a nullptr. Is this a correct assessment? And what is a proper way to possess a pawn and do all initialization stuff inside a respective PlayerController when working on multiplayer game (especially using Enhanced input)?