Download

[Help] Using If statements inside SetupPlayerInputComponent or something similar.

I have the following code inside my Pawn class’ SetupPlayerInputComponent()


InputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::StartGrowing);

The StartGrowing function it calls is just a function that sets the bool bGrowing to true.
I have another function that sets the same bool (bGrowin) to false.

I don’t want to set it to false on IE_Released, I want to make it so if it’s true, the “Grow” key will set it to false, and if it’s false, it will set it to true.

For that I could simply make an if statement like so


if (!bGrowing)
InputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::StartGrowing)
else
InputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::StopGrowing)


(StopGrowing sets bGrowing to false)

But when I do it inside the


void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)

The code doesn’t do anything when I press the “Grow” key, as if the lines are being ignored because they’re inside an if statement.

Is there any way I can do this thing? Am I missing something?
Any help would be greatly appreciated.
Thanks in advance!

I think what you are missing is that SetupPlayerInputComponent is called only once when your Pawn comes into being. You are treating it as if it gets called with every key press.

You’ll need to move your logic into your key handler. For instance:


InputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::ChangeGrowing)

void AMyPawn::ChangeGrowing()
{
   bGrowing = !bGrowing;
   // other stuff
}

Oh I see.
Thank you for clarifying that for me!