Shooter Game Crouch

I have added this code to the SetupPlayerInputComponent:

	InputComponent->BindAction("Crouch", IE_Pressed, this, &AShooterCharacter::Crouch);
	InputComponent->BindAction("UnCrouch", IE_Released, this, &AShooterCharacter::UnCrouch);

I’m getting this error and I’m not sure what I should be casting:

error C2664: 'FInputActionBinding &UInputComponent::BindAction<AShooterCharacter>(const FName,const EInputEvent,UserClass *,void (__cdecl AShooterCharacter::* )(void))' : cannot convert argument 4 from 'void (__cdecl ACharacter::* )(bool)' to 'void (__cdecl AShooterCharacter::* )(void)'
1>          with
1>          [
1>              UserClass=AShooterCharacter
1>          ]
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Can you please post the declaration for AShooterCharacter::Crouch? Is it defined in AShooterCharacter or ACharacter?

The reason you’re getting this error (As far as I can tell, from when it happened to me!) is because BindAction is expecting a function that takes no parameters, namely because as far as I’m aware there’s no real way to transmit parameters to a function from just a keypress.

As you can see, Crouch() expects a bool to be passed into it.

So basically you’re telling the game to call Crouch(bool bClientSimulation) whenever a key is pressed, but when that key is pressed the engine doesn’t know what to fill in for bClientSimulation. To get around this, you need a function that doesn’t take any parameters when it is called.

The way I got around this was by creating two functions:

void AShooterCharacter::execOnStartCrouch()
{
	if (CharacterMovement)
	{
		if (CanCrouch())
		{
			CharacterMovement->bWantsToCrouch = true;
		}
	}
}

void AShooterCharacter::execOnEndCrouch()
{
	if (CharacterMovement)
	{
		CharacterMovement->bWantsToCrouch = false;
	}
}

Then binding to THOSE functions. That way the functions can be properly bound, and by simply setting bWantsToCrouch on the CharacterMovementComponent, the engine will handle crouching for me.

Quick note, if I remember properly, crouching is still disabled in the CanEverCrouch() function of the MovementComponent. You’ll have to override that to enable crouching still, I think. If you can’t figure it out just post and I’ll take a look at my source and help you out.

Kopirat, I’m having an issue crouching as well in c++. I’m assuming it’s because the issue you stated above. How do I disable the CanEverCrouch() in c++?
Thanks,

Go into your movement component class and override CanEverCrouch to return true and you should be all set!

I’m still a little confused about this. I went into the MovementComponent.h, but I don’t see CanEverCrouch. Do I need to add something like:
virtual bool CanEverCrouch() OVERRIDE;?
Sorry I’m new to overriding. Trying to read up on it now.

Edit: I’m doing the first person shooter tutorial as well so I don’t have the fps template. (Thought I should mention this)

Edit 2: Looking through the code it looks like

 FORCEINLINE bool CanEverCrouch() const { return NavAgentProps.bCanCrouch; } 

but I’m unable to override canevercrouch from my fpscharacter.h, (because it’s not in the base class I assume?).

The Easy part is on PlayerPawn in the Editor and under Defaults->Movement Component->Nav Agent Props" Check “Can Crouch” This will allow you to turn it on, now, you’ll have to create your own animations,etc once this is done.