SetupInputComponent crash my project on play

Inside player controller:

void AGameplayPlayerController::SetupInputComponent(){
	InputComponent->BindAction("OPressed", IE_Pressed, this, &AGameplayPlayerController::OPressed);
	InputComponent->BindAxis("MouseX", this, &AGameplayPlayerController::InputAxisMouseX);
	InputComponent->BindAxis("MouseY", this, &AGameplayPlayerController::InputAxisMouseY);
}

And the definition of the call back functions

  void AGameplayPlayerController::InputAxisMouseY(float AxisValue){
	if (CurrentControllingDevice == ControllingDevice::mouse) AddPitchInput(AxisValue*MouseSensitivityCurrent);
}

void AGameplayPlayerController::InputAxisMouseX(float AxisValue){
	if (CurrentControllingDevice == ControllingDevice::mouse) AddPitchInput(AxisValue*MouseSensitivityCurrent);
}

void AGameplayPlayerController::OPressed(){
	UWidgetBlueprintLibrary::SetInputMode_UIOnly(this, MyMainMenu, true);
	bShowMouseCursor = true;
	MyMainMenu->AddToViewport();
}

Here you can see the exception.

I forgot to put the call stack.

I can’t see the functions bound to the MouseX and MouseY axes

Update now

I think that in OPressed(), the variable called ‘MyMainMenu’ might be still a nulltpr. Let’s try creating some protection. I’d go for if(!ensure(MyMainMenu!=nulltpr)) { return; } or if(MyMainMenu==nulltpr) { return; }

MyMainMenu may be nullptr, however what is generating the access violation is the name of the action or the axis.

Oh, you’re right of course. But I have noticed another possibility of a nullptr: directly in your SetupInputComponent() - probably your InputComponent is null. There’s my code:
if (GetPawn() == nullptr) { return; } auto PlayerInputComponent = GetPawn()->FindComponentByClass<UInputComponent>(); if (!ensure(PlayerInputComponent != nullptr)) { return; } PlayerInputComponent->BindAxis(TEXT("MoveForward"), this,...

Hi.

I followed your advice and the project no longer crash, however I’m not sure if SetupInputComponent () is being called.

Why do you think so? Doesn’t the binding work properly? If because of the SetupInputComponent() your project was crashing, it must be called. Maybe there are some logs logging out at the beginning?

I put some logs in there, but I can see them in the Output Log. Also the movement of the mouse is not being captured.

Hello there.

After looking at other posts I discovered that what was needed was to call the function of the parent class, Super :: SetupInputComponent (). This function initializes the InputComponent if it is equal to NULL.

1 Like