본문 바로가기

개인적인 팁

언리얼5 변경된 Input(EnhancedInput) 사용법 c++(2)

해당 프로젝트의 Build.cs로 가서 모듈을 추가해줘야 한다.

"EnhancedInput" 해당 모듈을 추가해 준다.

public UHorrorGame(ReadOnlyTargetRules Target) : base(Target)
    {
       PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
    
       PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" });

       // Uncomment if you are using Slate UI
       // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
       
       // Uncomment if you are using online features
       // PrivateDependencyModuleNames.Add("OnlineSubsystem");

       // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
    }
}

 

할당된 키를 입력처리 하는 부분은 기존과 동일하다.

Actor의  SetupPlayerInputComponent 에서 처리를 해도되고

virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;

 

PlayerController의 OnPossess 에서 해줘도 된다.

virtual void OnPossess(APawn* InPawn);

 

나는  Actor에서 처리 하였다.

Actor클래스를 추가하고 변수로 인풋액션, 인풋맵핑컨텐스트를 담을 변수를 추가한다.

UPROPERTY(Category=InputAction, EditAnywhere, meta=(AllowPrivateAccess = "true"))
TObjectPtr<UInputMappingContext> InputContext;

UPROPERTY(Category=InputAction, EditAnywhere, meta=(AllowPrivateAccess = "true"))
TObjectPtr<UInputAction> InputMoveAction;

액터 BP에 노출된 변수에 이전에 만든 Asset를 세팅하자.

 

 

해당 액터의 BeginPlaye에서 인풋맵핑컨텍스트를 추가해주자.

ULocalPlayer::GetSubsystem 를 통해 SubSystem에 접근 가능하다.

AddMappingContext에 인풋맵핑컨텍스트를 세팅해준다.

void AHG_PlayerCharacter::BeginPlay()
{
    Super::BeginPlay();

    TWeakObjectPtr<APlayerController> MyPlayerController = Cast<APlayerController>(GetController());
    if(MyPlayerController.IsValid())
    {
       TWeakObjectPtr<UEnhancedInputLocalPlayerSubsystem> EnhancedInputSystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(MyPlayerController.Get()->GetLocalPlayer());
       if(EnhancedInputSystem.IsValid())
       {
          EnhancedInputSystem->ClearAllMappings();
          EnhancedInputSystem.Get()->AddMappingContext(InputContext, 0);
       }
    }
}

 

 

바인딩 및 처리 방법은 이전과 거의 흡사하다.

바인딩된 함수의 인자로 다른것들도 받을수 있는데, 입력된키의 시간 정보도 받을수 있다.

일반적인경우는 InputActionValue로도 충분해 보인다.

DECLARE_DELEGATE_OneParam(FEnhancedInputActionHandlerValueSignature, const FInputActionValue&);
DECLARE_DELEGATE_OneParam(FEnhancedInputActionHandlerInstanceSignature, const FInputActionInstance&);   // Provides full access to value and timers
 
void AHG_PlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    TWeakObjectPtr<UEnhancedInputComponent> EnhancedCMP = Cast<UEnhancedInputComponent>(PlayerInputComponent);
    
    if(EnhancedCMP.IsValid())
    {
       EnhancedCMP.Get()->BindAction(InputMoveAction, ETriggerEvent::Triggered, this, &AHG_PlayerCharacter::MoveAction);
    }
}
void AHG_PlayerCharacter::MoveAction(const FInputActionValue& InActionValue)
{
    FVector2D MoveValue = InActionValue.Get<FVector2d>();

    if(GetController() && MoveValue.X != 0.f)
    {
       FVector ForwardVec = GetActorForwardVector();
       AddMovementInput(ForwardVec, MoveValue.X);
    }

    if(GetController() && MoveValue.Y != 0.f)
    {
       FVector RightVec = GetActorRightVector();
       AddMovementInput(RightVec, MoveValue.Y);
    }
}

 

 

또 해당 이번 Input기능에 디버깅 기능도 추가되었는데.
콘솔 명령어로 ShowDebug EnhancedInput 입력하면된다.

IA_Look 마우스로 회전값추가 했더니 같이 보인다.