Hi, I’m new to C++ in UE 5.3.2 and I’m trying to reduce boilerplate code I’m typing. Typically, when I need to know (inside a Tick for example) whether a key is pressed, I would add a bool field, one callback setting that bool field to true and one callback setting that bool field to false. Example:
void APPReCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
EnhancedInputComponent->BindAction(AscendAction, ETriggerEvent::Started, this, &APPReCharacter::HandleAscendInputStart);
EnhancedInputComponent->BindAction(AscendAction, ETriggerEvent::Completed, this, &APPReCharacter::HandleAscendInputEnd);
// ...
void APPReCharacter::HandleAscendInputStart(const FInputActionValue& Value)
{
bAscendKeyIsPressed = true;
}
void APPReCharacter::HandleAscendInputEnd(const FInputActionValue& Value)
{
bAscendKeyIsPressed = false;
}
// ... use that bool property elsewhere
This is a lot of code for just one field, so I’ve tried to minimize this by doing the following:
void APPReCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
if (UEnhancedInputComponent* Eic = Cast<UEnhancedInputComponent>(PlayerInputComponent))
{
EnhancedInputComponent = Eic;
// ...
void APPReCharacter::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
bool bAscendKeyIsPressed = EnhancedInputComponent->GetBoundActionValue(AscendAction).Get<bool>();
Hovewer, the latter approach doesn’t seem to work the way I’ve imagined it would’ve, bAscendKeyIsPressed is always false. The mapping itself is fine, as the first approach with callback methods is working as expected. Is there another option that I’m missing?