how can i write an custom Input Action in unreal c++

so i have made a custom input action with the key I, and i wanna know how i can write this BP code in c++

image

1 Like

This can definitely be done more elegantly, but I would defer to how Lyra handles this if you want a better solution. This should work however (keep in mind that you need to set the input action in the actor’s properties):

.h

UCLASS()
class MYGAME_API AMyActor : public AActor
{
    GENERATED_BODY()

    public:

    virtual void SetupPlayerInputComponent(UInputComponent* InputComponent);

    void OnToggleInventory();

    UPROPERTY(EditAnywhere)
    UInputAction* ToggleInventory;
}

.cpp

void AMyActor::SetupPlayerInputComponent(UInputComponent* InputComponent)
{
    BindAction(ToggleInventory, ETriggerEvent::Triggered, this, &AMyActor::ToggleInventory);
}

void AMyActor::ToggleInventory()
{
    // Do things here
}

Let me know if I missed anything, since I haven’t tested this.

1 Like

i get error on the UInputAction variable and the
BindAction(ToggleInventory, ETriggerEvent::Triggered, this, &AMyActor::ToggleInventory);

1 Like
FInputActionBinding CurBinding& = InputComponent->BindAction(TEXT("OpenMenu"), EInputEvent::IE_Released, this, &APlayerControllerGame::ActOnOpenMenu);
CurBinding.bConsumeInput = true;
CurBinding.bExecuteWhenPaused = true;

Where “this” in the example == APlayerController and the implemented method on it ActOnOpenMenu is marked UFUNCTION. Added example of how to set some parameters on the binding but they have defaults . PlayerControllers and Pawns are the common classes to use InputComponents on. If this would be implemented on a UserWidget, forget about InputComponents because they have their own input routing system:

Widget input: It SMELLS

1 Like