I was trying to implement an Enhanced Input System for my Pawn C++ class. I have a base class (ABasePawn), that includes diferent components and a child class (ATank), where i am implementing input system. Here is code for the class:
// Tank.h
#pragma once
#include "CoreMinimal.h"
#include "BasePawn.h"
#include "InputActionValue.h"
#include "Tank.generated.h"
UCLASS()
class TOONTANKS_API ATank : public ABasePawn
{
GENERATED_BODY()
public:
ATank();
void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent);
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = "Components")
class USpringArmComponent* SpringComp;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = "Components")
class UCameraComponent* CameraComp;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditDefaultsOnly, Category="Enhanced Input")
class UInputMappingContext* InputMapping;
UPROPERTY(EditDefaultsOnly, Category = "Enhanced Input")
class UInputAction* TestAction;
UPROPERTY(EditDefaultsOnly, Category = "Enhanced Input")
class UInputAction* IA_Fire;
UPROPERTY(EditDefaultsOnly, Category = "Enhanced Input")
class UInputAction* IA_Move;
UPROPERTY(EditDefaultsOnly, Category = "Enhanced Input")
class UInputAction* IA_Rotate;
UPROPERTY(EditDefaultsOnly, Category = "Enhanced Input")
class UInputAction* IA_Turn;
void TestInput();
void Move(const FInputActionValue& Value);
};
// Tank.cpp
#include "Tank.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "InputMappingContext.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
ATank::ATank()
{
SpringComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
SpringComp->SetupAttachment(RootComponent);
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera Comp"));
CameraComp->SetupAttachment(SpringComp);
}
void ATank::BeginPlay()
{
Super::BeginPlay();
// Add input mapping context
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
// add input context
Subsystem->AddMappingContext(InputMapping, 0);
}
}
}
void ATank::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* Input = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
// bind input to funtions
Input->BindAction(TestAction, ETriggerEvent::Triggered, this, &ATank::TestInput);
Input->BindAction(IA_Move, ETriggerEvent::Triggered, this, &ATank::Move);
}
}
void ATank::TestInput()
{
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, "Pressed Input Action");
}
void ATank::Move(const FInputActionValue& Value)
{
const FVector2D MovementVector = Value.Get<FVector2D>();
UE_LOG(LogTemp, Log, TEXT("MovementVector: %s"), *MovementVector.ToString());
FVector Forward = GetActorForwardVector();
AddMovementInput(Forward, MovementVector.Y);
}
To use the pawn I created a BP class based on the Tank class and filled in all the necessities
Here is also the part with IA_Move from IMC_PLayer
Both TestInput()
and Move()
functions are called properly, and the debug code is printed correctly. However, AddMovementInput()
for some reason is not working.
Additionally, I attach a screenshot with debug information to show that input system is properly connected to the pawn.
I will be very grateful if you can help me with this problem. I can’t find a solution on my own for the second day