본문 바로가기

Unreal/개인플젝

인풋컨포넌트 분리함

애님비피 달아줬다.

 

 

pc에서 인풋 처리하는 부분을 따로 빼어서 조금 더 깔끔하게 정리했다

 

// Fill out your copyright notice in the Description page of Project Settings.
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MLPlayerController.generated.h"

struct FInputActionInstance;
class UMLInputProcessComponent;
/**
 * 
 */
UCLASS()
class CROPOUTSAMPLEPROJECT_API AMLPlayerController : public APlayerController
{
	GENERATED_BODY()
	
protected:

	AMLPlayerController(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
	virtual void BeginPlay() override;
	virtual void SetupInputComponent() override;
	virtual void OnPossess(APawn* aPawn) override;

	
public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float BaseTurnRate;

	/** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera)
	float BaseLookUpRate;

	UPROPERTY(BlueprintReadWrite, EditAnyWhere)
	TObjectPtr<UMLInputProcessComponent> InputProcessComponent;


	//UPROPERTY(BlueprintReadWrite, EditAnyWhere)
	//	TSubclassOf<UMLInputProcessComponent> InputProcessComponentClass;
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "MLPlayerController.h"
#include "CropoutSampleProject.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"

#include "MLInputProcessComponent.h"
#include "InputAction.h"
#include "InputMappingContext.h"


// > Protected ------

AMLPlayerController::AMLPlayerController(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	OverridePlayerInputClass = UEnhancedInputComponent::StaticClass();

	InputProcessComponent = CreateDefaultSubobject<UMLInputProcessComponent>(TEXT("InputProcessComponent"));

}

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

	if (const ULocalPlayer* LP = GetLocalPlayer())
	{
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = LP->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>())
		{
			Subsystem->AddMappingContext(InputProcessComponent->InputMapping, 0);
		}
	}
}

void AMLPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();
	InputProcessComponent->SetUpInputSetting(Cast<UEnhancedInputComponent>(InputComponent));
}

void AMLPlayerController::OnPossess(APawn* aPawn)
{
	Super::OnPossess(aPawn);
	InputProcessComponent->OnPossessedPawn(aPawn);;
}
// < Protected -----

 

인풋 자체를 처리하는게 아니라, 인풋 바인딩이나 호출할 함수를 처리하는 컨포넌트이므로 InputProcessComp로 지었다.

 

// Fill out your copyright notice in the Description page of Project Settings.
#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "MLInputProcessComponent.generated.h"

struct FInputActionInstance;
class UInputMappingContext;
class UInputAction;
class UEnhancedInputComponent;
class APawn;
/**
 * 
 */

UENUM(BlueprintType)
enum class EInputType :uint8
{
	None,
	MoveRight,
	Zoom,
	Max
};

USTRUCT(BlueprintType)
struct FActionInfo
{
	GENERATED_BODY()
public:
	UPROPERTY(BlueprintReadWrite, EditAnywhere)
	UInputAction* InputAction;
	
	UPROPERTY(BlueprintReadWrite, EditAnywhere)
	EInputType InputType;
};

UCLASS()
class CROPOUTSAMPLEPROJECT_API UMLInputProcessComponent : public UActorComponent
{
	GENERATED_BODY()
public:
	void SetUpInputSetting(UEnhancedInputComponent* InInputComponent);
	void OnPossessedPawn(APawn* Pawn);
private:
	void MoveRight(const FInputActionInstance& Instance);
	void Zoom(const FInputActionInstance& Instance);
	void MoveForward(float Value);
private:
	UPROPERTY(Transient )
	APawn* CurrentPawn;
public:
	UPROPERTY(EditAnywhere, Category = Input)
		UInputMappingContext* InputMapping;

	UPROPERTY(EditAnywhere, Category = Input)
		TArray<FActionInfo> ActionInfos;
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "MLInputProcessComponent.h"
#include "GameFramework/Pawn.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "InputAction.h"
#include "InputMappingContext.h"




void UMLInputProcessComponent::SetUpInputSetting(UEnhancedInputComponent* InInputComponent)
{
	if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(InInputComponent))
	{
		for (const FActionInfo& Info : ActionInfos)
		{
			switch (Info.InputType)
			{

			case EInputType::MoveRight:
			{
				EnhancedInputComponent->BindAction(Info.InputAction, ETriggerEvent::Triggered, this, &UMLInputProcessComponent::MoveRight);
			}
			break;

			case EInputType::Zoom:
			{
				EnhancedInputComponent->BindAction(Info.InputAction, ETriggerEvent::Triggered, this, &UMLInputProcessComponent::Zoom);
			}
			break;
			case EInputType::Max:
				break;
			default:
				break;
			}
		}
	}
}
void UMLInputProcessComponent::OnPossessedPawn(APawn* Pawn)
{
	CurrentPawn = Pawn;
}
///////
// < Protected -----

// > private -----
#pragma region INPUT ACTIONS

void UMLInputProcessComponent::MoveForward(float Value)
{

	if (CurrentPawn == nullptr)
	{
		return;
	}
	if (Value == 0.0f)
	{
		return;
	}

	// find out which way is forward
	//CurrentPawn->GetForwardVector();
//	const FRotator Rotation = GetControlRotation();
//	const FRotator YawRotation(0, Rotation.Yaw, 0);
//
	// get forward vector
	const FVector Direction = CurrentPawn->GetActorTransform().GetUnitAxis(EAxis::Y);// FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	CurrentPawn->AddMovementInput(Direction, Value);
}

void UMLInputProcessComponent::MoveRight(const FInputActionInstance& Instance)
{
	if (CurrentPawn == nullptr)
	{
		return;
	}
	float Value = Instance.GetValue().Get<float>();
	if (Value == 0.0f)
	{
		return;
	}


	// find out which way is right
	//const FRotator Rotation = GetControlRotation();
	//const FRotator YawRotation(0, Rotation.Yaw, 0);

	// get right vector 
	const FVector Direction = CurrentPawn->GetActorTransform().GetUnitAxis(EAxis::Y);// FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
	// add movement in that direction
	CurrentPawn->AddMovementInput(Direction, Value);
}


void UMLInputProcessComponent::Zoom(const FInputActionInstance& Instance)
{
	float Value = Instance.GetValue().Get<float>();
	if (Value == 0.0f)
	{
		return;
	}
	//카메라 지금 폰BP에 있음. 카메라시스템 연결 뒤 만들기
	//CameraSystem->Zoom(Value);
	// or
	// GameEventSystem->SendEvent_Zoom(Value);
}
#pragma endregion
// < private -----

(수정여ㅖ정)()

 

근데 생성자에서 분명 만들어주는데 inputProcssCompnent 가 계속 null이라서 오잉 이거 내일 봐야지

'Unreal > 개인플젝' 카테고리의 다른 글

업데이트  (0) 2023.11.24
이벤트시스템  (1) 2023.11.24
언리얼버그속상해 - ActorComponent가 BeginPlay 에서 null일때  (2) 2023.09.07
[언리얼] 3번째. 스포너  (0) 2023.09.02
개인플젝 시작했다  (0) 2023.08.31