Disable Zoom function while sprinting

Hi!

I’ve been extending on the Simple Shooter, and I’ve now got working Zoom in / Out and Sprint/StopSprinting functions, but I would like to disable the Zoom while Sprinting. I have set a new FOV when we enter sprint in case we were already zoomed in, but halfway through the sprint you can just press zoom again and it will zoom in. I want to disable the zoom entirely during sprint.

I figured this would be the easiest solution:

void AShooterCharacter::Sprint()

{

GetCharacterMovement()->MaxWalkSpeed *= SprintSpeedMultiplier;

IsCharacterSprinting = true;

int NewFOV = 90;

CameraComp->SetFieldOfView(NewFOV);



// Disable Zoom input.
PlayerInputComponent->RemoveActionBinding("Zoom", IE_Pressed);

}

void AShooterCharacter::StopSprinting()

{

GetCharacterMovement()->MaxWalkSpeed /= SprintSpeedMultiplier;

IsCharacterSprinting = false;

int NewFOV = 90;

CameraComp->SetFieldOfView(NewFOV);



// Enable Zoom input.
PlayerInputComponent->BindAction(TEXT("Zoom"), EInputEvent::IE_Pressed, this, &AShooterCharacter::ZoomIn);

}

The error I’m getting is:
D:\Projects\simpleshooter_extended\Source\SimpleShooter\ShooterCharacter.cpp(152): error C2065: ‘PlayerInputComponent’: undeclared identifier.

I have no idea how to access the PlayerInputComponent where all the ActionBindings live.

Hope you can point me in the right direction, or a hint where I should be looking for a proper implementation.

Thanks for taking a look!

The variable name is InputComponent not PlayerInputComponent. The latter was the argument that is passed to SetupInputComponent.

I’ve renamed the variable “PlayerInputComponent” to “InputComponent”.

Updated in the header file (public):

virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

Updated in the SetupPlayerInputComponent Function:

void AShooterCharacter::SetupPlayerInputComponent(UInputComponent* InputComponent)

{

Super::SetupPlayerInputComponent(InputComponent);

InputComponent->BindAxis(TEXT("MoveForward"), this, &AShooterCharacter::MoveForward);

InputComponent->BindAxis(TEXT("LookUp"), this, &APawn::AddControllerPitchInput);

InputComponent->BindAxis(TEXT("MoveRight"), this, &AShooterCharacter::MoveRight);

InputComponent->BindAxis(TEXT("LookRight"), this, &APawn::AddControllerYawInput);

InputComponent->BindAxis(TEXT("LookUpRate"), this, &AShooterCharacter::LookUpRate);

InputComponent->BindAction(TEXT("Jump"), EInputEvent::IE_Pressed, this, &ACharacter::Jump);

InputComponent->BindAxis(TEXT("LookRightRate"), this, &AShooterCharacter::LookRightRate);

InputComponent->BindAction(TEXT("Shoot"), EInputEvent::IE_Pressed, this, &AShooterCharacter::Shoot);

InputComponent->BindAction(TEXT("Zoom"), EInputEvent::IE_Pressed, this, &AShooterCharacter::ZoomIn);

InputComponent->BindAction(TEXT("Zoom"), EInputEvent::IE_Released, this, &AShooterCharacter::ZoomOut);

InputComponent->BindAction(TEXT("Sprint"), EInputEvent::IE_Pressed, this, &AShooterCharacter::Sprint);

InputComponent->BindAction(TEXT("Sprint"), EInputEvent::IE_Released, this, &AShooterCharacter::StopSprinting);

}

Updated it in the Sprint and StopSprinting functions:

void AShooterCharacter::Sprint()

{

GetCharacterMovement()->MaxWalkSpeed *= SprintSpeedMultiplier;
IsCharacterSprinting = true;

int NewFOV = 90;

CameraComp->SetFieldOfView(NewFOV);

// Disable Zoom input.
InputComponent->RemoveActionBinding("Zoom", IE_Pressed);

}

void AShooterCharacter::StopSprinting()

{

GetCharacterMovement()->MaxWalkSpeed /= SprintSpeedMultiplier;
IsCharacterSprinting = false;

int NewFOV = 90;

CameraComp->SetFieldOfView(NewFOV); 

// Enable Zoom input.
InputComponent->BindAction(TEXT("Zoom"), EInputEvent::IE_Pressed, this, &AShooterCharacter::ZoomIn);

}

Still getting the same error:

D:\Projects\simpleshooter_extended\Source\SimpleShooter\ShooterCharacter.cpp(52): error C4458: declaration of ‘InputComponent’ hides class member
E:\Engines\UE\UE_4.25\Engine\Source\Runtime\Engine\Classes\GameFramework/Actor.h(568): note: see declaration of ‘AActor::InputComponent’

I have even renamed the variable to MyVeryCustomInputComponent at some point and I’m still getting this error, it won’t let me access the InputComponent.

You renamed too much. The parameter name for SetupPlayerInputComponent should be kept and that’s what the error message is talking about.

AActor has a public variable called InputComponent. SetupPlayerInputComponent has a parameter of the same name which means the parameter hides the member variable of the same name. Meaning uses of InputComponent refer to the parameter and not the member variable. This is known as variable shadowing and is a warning that Unreal treats as an error.

So in summary SetupPlayerInputComponent’s parameter and the uses within that function should be renamed back to PlayerInputComponent. Uses outside of that function should be InputComponent

If you have created an input component yourself you shouldn’t. As stated AActor already has a public InputComponent variable.

Ah I see, thanks! I should dive more into these classes and structure. I was just blindly staring at the paramater.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms