EnhancedInput Action "Started" doesnt work on C++?

When I use ‘Triggered’ instead of ‘Started’ everything works. But I specifically need ‘Started’. I did the same thing on blueprints and everything works. Where is the issue?

Your code is incomplete. You should post your header.

Does ThisClass::I_OnTurnL have a UFUNCTION decorator?

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = “Inputs”, meta = (DisplayThumbnail = false))
TObjectPtr “UInputAction” TurnLAction; (I put the buckets because it doesn’t skip with <>)

virtual void I_OnTurnL(const FInputActionValue& ActionValue);

UFUNCTION() does not change. I just checked it out

Solution:

EnhancedInput->BindAction(TurnLAction, ETriggerEvent::Started, this, &ThisClass::I_OnTurnLStarted);
EnhancedInput->BindAction(TurnLAction, ETriggerEvent::Completed, this, &ThisClass::I_OnTurnLCompleted);

void ATemp::I_OnTurnLStarted(const FInputActionValue& ActionValue)
{
TurnL(45.0f);
}

void ATemp::I_OnTurnLCompleted(const FInputActionValue& ActionValue)
{
StopTurnL();
}

I asked you a question to Google AI, here is the answer : The problem is likely with how you’re obtaining the ActionValue and using it to determine the “started” state. ETriggerEvent::Started should only be called once when the button is pressed, and ETriggerEvent::Completed only once when the button is released. Your logic assumes that ActionValue contains a boolean value that reflects the current state (pressed or released), but that’s not always the case.

Here’s why:

  1. ActionValue.Get() Doesn’t Return a Boolean for ETriggerEvent::Started and Completed: For the triggered events Started and Completed, ActionValue.Get() might not directly return a boolean. The returned value depends on the type of Action you’re using (e.g., Axis, Digital, etc.). For digital Actions, it might return a float (1.0 when pressed, 0.0 when released). For analog Actions, it might return values between -1 and 1.

  2. Incorrect Interpretation of the Value: Your check if (bStarted) assumes that bStarted will be true when the button is pressed and false when released. But if ActionValue.Get() returns, for example, a float, any non-zero value will be interpreted as true. This can lead to TurnL and TurnR being called multiple times in a row while the button is held down.

Solution:

Instead of using ActionValue.Get() to get the state, it’s better to rely on ETriggerEvent::Started and ETriggerEvent::Completed themselves as state markers. Separate the logic into two separate handlers: one for the press and one for the release.

Here’s the corrected code:

// Separate functions for Started and Completed
void ATemp::I_OnTurnLStarted(const FInputActionValue& ActionValue)
{
    UE_LOG(LogTemp, Warning, TEXT("Turn L Started"));
    TurnL(45.0f);
}

void ATemp::I_OnTurnLCompleted(const FInputActionValue& ActionValue)
{
    UE_LOG(LogTemp, Warning, TEXT("Turn L Stopped"));
    StopTurnL();
}

void ATemp::I_OnTurnRStarted(const FInputActionValue& ActionValue)
{
    UE_LOG(LogTemp, Warning, TEXT("Turn R Started"));
    TurnR(45.0f);
}

void ATemp::I_OnTurnRCompleted(const FInputActionValue& ActionValue)
{
    UE_LOG(LogTemp, Warning, TEXT("Turn R Stopped"));
    StopTurnR();
}

// Binding to Events
EnhancedInput->BindAction(TurnLAction, ETriggerEvent::Started, this, &ThisClass::I_OnTurnLStarted);
EnhancedInput->BindAction(TurnLAction, ETriggerEvent::Completed, this, &ThisClass::I_OnTurnLCompleted);
EnhancedInput->BindAction(TurnRAction, ETriggerEvent::Started, this, &ThisClass::I_OnTurnRStarted);
EnhancedInput->BindAction(TurnRAction, ETriggerEvent::Completed, this, &ThisClass::I_OnTurnRCompleted);

Use code with caution.C++

Explanation:

  • We created separate functions to handle the Started and Completed events.

  • In these functions, we no longer use ActionValue.Get(), as the event itself is the state indicator.

  • We changed the binding so that each function is called only for the corresponding event (button press or release).

With this change, ETriggerEvent::Started and ETriggerEvent::Completed should work correctly, and your turning logic should only execute once on button press and once on button release.

If the problem persists, make sure your TurnLAction and TurnRAction are properly configured in the Input Settings (Project Settings → Input) and that they have the correct type (Digital, Axis, etc.).

8.8s

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.