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

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