- UEnhancedInputComponent로 형변환
- 원래는 UInputComponent인데 언리얼5에서 도입 향상된 입력시스템으로 바꿔주는 작업을 해줘야 한다. Enhanced를 추가해주면 된다.
- UEnhanedInputComponent는 UInputComponent의 자식클래스이다.
- 기존 가상함수가 고정으로 있기때문에 변경할 수는 없고, 형변환을 통해 사용해야 한다. 함수의 시그니쳐를 바꿔버리면 오버라이딩이 되지 않는다.
- 언리얼 엔진은 기본적으로 어떤 입력 컴포넌트가 사용될 지 모르기 때문에 UInputComponent*를 전달한다.
- 나중에 다른 입력 시스템이 나올 수도 있기 때문에 유연한 대처가 가능하다..
- 네 개의 매개변수
- 연결할 입력 액션(IA) ex) PlayerController→MoveAction
- 어떤 상황에 이 액션을 실행할지 ex) ETriggerEvent::Triggered
- 어떤 객체의 함수를 호출할 지 ex) this
- 어떤 함수를 호출할 지 (주소의 형태_함수포인터) ex) &APracticeCharacter::Move
- C++에서 함수포인터를 사용할 때는 같은 클래스에 있더라도 어던 클래스의 어떤 함수인지 명확하게 지정해줘야 한다.
PracticeCharacter.h
struct FInputActionValue; // 액션바인딩을 할 때는 이 구조체를 매개변수로 사용한다.
// 전방선언
UFUNCTION()
void Move(const FInputActionValue& value);
UFUNCTION()
void Look(const FInputActionValue& value);
UFUNCTION()
void StartJump(const FInputActionValue& value);
UFUNCTION()
void StopJump(const FInputActionValue& value);
UFUNCTION()
void StartSprint(const FInputActionValue& value);
UFUNCTION()
void StopSprint(const FInputActionValue& value);
PracticeCharacter.cpp
#include "EnhancedInputComponent.h"
void APracticeCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInput = Cast<UEnhancedInputComponent>(PlayerInputComponent))
{
if (APracticePlayerController* PlayerController = Cast<APracticePlayerController>(GetController()))
{
if (PlayerController->MoveAction)
{
EnhancedInput->BindAction(
PlayerController->MoveAction,
ETriggerEvent::Triggered,
this,
&APracticeCharacter::Move
);
}
if (PlayerController->LookAction)
{
EnhancedInput->BindAction(
PlayerController->LookAction,
ETriggerEvent::Triggered,
this,
&APracticeCharacter::Look
);
}
if (PlayerController->JumpAction)
{
EnhancedInput->BindAction(
PlayerController->JumpAction,
ETriggerEvent::Triggered,
this,
&APracticeCharacter::StartJump
);
EnhancedInput->BindAction(
PlayerController->JumpAction,
ETriggerEvent::Completed,
this,
&APracticeCharacter::StopJump
);
}
if (PlayerController->SprintAction)
{
EnhancedInput->BindAction(
PlayerController->SprintAction,
ETriggerEvent::Triggered,
this,
&APracticeCharacter::StartSprint
);
EnhancedInput->BindAction(
PlayerController->SprintAction,
ETriggerEvent::Completed,
this,
&APracticeCharacter::StopSprint
);
}
}
}
}