アンリアる! C++入門編 ~対話形式で学ぶUnreal Engine~
BOOTHでUnreal Engine C++入門書を販売していますUnreal Engine上でC++を使って開発するために必要となる基礎知識を一冊の本にまとめた本です。 対話形式によるわかりやすい説明を目指しました。無料の試し読みも可能ですので、ぜひ読んでみてください!
[UE4] Receive input in Actor

Actors can not receive the player input.
It is common to use Pawn to receive the player input.
However, you may want to receive the input in Actor as well.

This article explains how to receive input from the player in Actor, in both Blueprint and C++.

Case: Blueprint

Enable Input

In the case of Blueprint, open the Blueprint editor of Actor and set Auto Receive Input located in Input on the Details tab to Player 0. Then, the Player Controller will be able to receive the player’s input.

Enabling Input (Blueprint)

Definition of Input Processing

You can process the player’s input by defining the process of events while the player’s input is enabled.

We will show an example of defining the process for an input, which assumes that "AxisA" is set for Axis and "ActionB" for Action.
For "AxisA" input, place an "InputAxis AxisA" node and define the process you want to execute.
Similarly, for "ActionB", place an "InputAxis ActionB" node and define the process.

Defining the process for the input (Blueprint)

Case: C++

Enable Input

In the case of C++, you need to override the member function BeginPlay and call the member function EnableInput defined in the AActor class to receive input from the player.
The PlayerController must be passed as an argument.
In this example, the first PlayerController is passed.

void AInputEnabledActor::BeginPlay()
{
    Super::BeginPlay();
    // Enable Input.
    EnableInput(GetWorld()->GetFirstPlayerController());
    // ...
}

Definition of Input Processing

When an input is enabled, the member variable InputComponent of the UInputComponent defined in the AActor class will be set a value (If EnableInput is not called, InputComponent becomes nullptr).
Once the input is enabled, only you need to do is defining the process for the input same as Pawn.

We will define the process for the input immediately after the input is enabled with BeginPlay.

It is also possible to set the behaviour for the defined input same as Pawn.
In this example, we define bConsumeInput, which determines whether the input is propagated to other objects.
And we define bExecuteWhenPaused, which determines whether the input can be received while paused.

void AInputEnabledActor::AxisAProcess(float AxisValue)
{
    FVector NewLocation = GetActorLocation();
    NewLocation += FVector(0.0, AxisValue, 0.0);
    SetActorLocation(NewLocation);
}
void AInputEnabledActor::ActionBPressedProcess()
{
    UKismetSystemLibrary::PrintString(NULL, TEXT("ActionB Pressed"));
}
void AInputEnabledActor::BeginPlay()
{
    Super::BeginPlay();
    // Enable Input.
    EnableInput(GetWorld()->GetFirstPlayerController());
    if (InputComponent)
    {
        // Register the process of input "AxisA".
        FInputAxisBinding& AxisBinding = InputComponent->BindAxis("AxisA", this, &AInputEnabledActor::AxisAProcess);
        AxisBinding.bConsumeInput = false;
        AxisBinding.bExecuteWhenPaused = true;
        // Register the process of input "ActionB".
        FInputActionBinding& ActionBinding = InputComponent->BindAction("ActionB", IE_Pressed, this, &AInputEnabledActor::ActionBPressedProcess);
        ActionBinding.bConsumeInput = false;
        ActionBinding.bExecuteWhenPaused = true;
    }
}

Summary

This article explained how to receive player input in Actor.

We have seen that both Blueprint and C++ require the Player Controller need to be set up properly in order to receive input.
Once the Player Controller has been set up, the rest procedures is the same as with Pawn.