how to use enhanced input for press, hold release

I am attempting to set up enhanced input for my game to allow the following:

  1. Tap the E key and the player fires the weapon once.
  2. Tap and hold the E key to initiate automatic fire and cease when the key is released.

I am doing this in C++.

How can I make this happen with Enhanced Input?

Hi,

Hope you’re doing well.

  1. Simple make a BindAction as Trigged type, e.g.
    EnhancedInputComponent->BindAction(ShotAction, ETriggerEvent::Triggered, this, &AGameCharacter::Shot);

  2. In the mapping context make the final ajustment adding two triggers, pressed and hold.


    You can also set the Threshold to adjust the time response.

Hope that helps - take care!

Thank you.

But I’m still confused.

I understand how &AGameCharacter::Shot gets called when the button gets pressed and released for a single shot. But how does the code know when a Hold is finished so I can stop firing?

Don’t worry, that’s part of the journey.

Since we’ve set two triggers in the mapping context, it will trigger with a simple press or hold.

Pressed will be called once you press the key.
Hold will be called while the key is pressed (holding).

By the moment you release the key, hold will stop.

Do you need any release implementation? I think the current implementation meets your specifications.

Maybe I’m just being especially dense on this:
Here are my bindings:
image
Here are my methods being called:


Here is my Attack input action:

and here is my IMC:

The problem is that my AOrionCharacter::StopAttacking() is never called.

Oh, I see.

That’s stranger, your EndAttack should be being called.

Are you sure your UE_LOG is not being printed on your Ouput Log?

Try one of the bellow implementation (or both).

  1. Add trigger released in your Input Mapping Context

  2. Add trigger released in your Input Action

What is your unreal version? I’m using 5.5 and at my computer is working just fine, Look:

// Input
EnhancedInputComponent->BindAction(ShotAction, ETriggerEvent::Triggered, this, &AClimbingSystemCharacter::Shot);
EnhancedInputComponent->BindAction(ShotAction, ETriggerEvent::Completed, this, &AClimbingSystemCharacter::EndShot);

void AClimbingSystemCharacter::Shot()
{
	Debug::Print("Shot", FColor::Green);
}

void AClimbingSystemCharacter::EndShot()
{
	Debug::Print("End Shot", FColor::Red);
}

TIP: Cast can be “heavy operation”, so I suggest you save at least one of them, e.g.:

if (AOrionCharacter* ControlledCharacter = Cast<AOrionCharacter>(GetPawn()))
{
	ControlledCharacter->AttackButtonReleased();
}

My Unreal version is 5.5.4.

I have no idea how to print UE_LOG messages to anywhere but the Output Window :slight_smile:

I am restarting both VS and UE and deleted Binaries, Intermediate and Saved as well as the cache directory. Rebuilding VS files and restarting UE and then VS.

Will try your suggestions next.

Adding Released to the InputMappingContext has solved the problem. Can you please explain?

Happy to know that.

So the Input Mapping Context is handling Release trigger event, such as Pressed and Hold, that’s all.

Are you saying that Hold does not fire off a Completed event when the key is released?

No way.

Because you’ve bound the release here.

c5f5c735a3f0ed7239ffc133d7e904fb75f4a82c_2_690x25

Without this Completed, your StopAttacking() could not be called. I just said the Input Mapping Context is handling the Released fire as well.

That’s what it seems.