ShaunM
(ShaunM)
June 21, 2025, 5:00am
1
I am attempting to set up enhanced input for my game to allow the following:
Tap the E key and the player fires the weapon once.
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?
SpecZynk
(SpecZynk)
June 21, 2025, 12:44pm
2
Hi,
Hope you’re doing well.
Simple make a BindAction as Trigged type, e.g.
EnhancedInputComponent->BindAction(ShotAction, ETriggerEvent::Triggered, this, &AGameCharacter::Shot)
;
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!
ShaunM
(ShaunM)
June 21, 2025, 6:47pm
3
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?
SpecZynk
(SpecZynk)
June 21, 2025, 6:56pm
4
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.
ShaunM
(ShaunM)
June 21, 2025, 7:53pm
5
Maybe I’m just being especially dense on this:
Here are my bindings:
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.
SpecZynk
(SpecZynk)
June 21, 2025, 8:33pm
6
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).
Add trigger released
in your Input Mapping Context
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();
}
ShaunM
(ShaunM)
June 21, 2025, 8:51pm
7
My Unreal version is 5.5.4.
I have no idea how to print UE_LOG messages to anywhere but the Output Window
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.
ShaunM
(ShaunM)
June 21, 2025, 8:56pm
8
Adding Released to the InputMappingContext has solved the problem. Can you please explain?
SpecZynk
(SpecZynk)
June 21, 2025, 9:00pm
9
Happy to know that.
So the Input Mapping Context
is handling Release
trigger event, such as Pressed
and Hold
, that’s all.
ShaunM
(ShaunM)
June 21, 2025, 9:03pm
10
Are you saying that Hold does not fire off a Completed event when the key is released?
SpecZynk
(SpecZynk)
June 21, 2025, 9:08pm
11
No way.
Because you’ve bound the release here.
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.