언리얼 엔진/C++

언리얼엔진 C++ 반환 값이 2개 이상일 경우에 사용하는 TTuple

mane 2022. 1. 2. 13:56
728x90
반응형

블루프린트에서 입력 파라미터와 출력 파라미터는 2개 이상 만들 수 있습니다.

 블루프린트에서 만든 함수를 C++로 작성할 때 입력 파라미터는 2개 이상 만드는 것은 쉽지만 출력 파라미터. 즉, 반환값을 2개 이상으로 만들 때는 구조체를 이용해야 하나 싶었습니다.

 그러나 반환하는 것 때문에 일일이 구조체를 만들기에는 효율성이 떨어지는것 같아 찾아보니 튜플이라는 자료형을 알게 되었습니다.

 참고로 TTuple은 UObject 클래스가 아니기 때문에 UFUNCTION과 UPROPERTY를 사용할 수 없습니다.

 

TTuple<float, float> FixDiagonalGamepadValues(float ForwardAxis, float RightAxis) const;

UFUNCTION(BlueprintCallable, Category = "CharacterMovement")
void PlayerMovementInput(bool bIsForwardAxis);

<헤더 파일>

 

TTuple<float, float> APCEPlayerCharacter::FixDiagonalGamepadValues(float ForwardAxis, float RightAxis) const
{
	float OutForwardAxis = ForwardAxis * FMath::GetMappedRangeValueClamped(FVector2D(0.0f, 0.6f), FVector2D(1.0f, 1.2f), FMath::Abs(RightAxis));
	float OutRightAxis = RightAxis * FMath::GetMappedRangeValueClamped(FVector2D(0.0f, 0.6f), FVector2D(1.0f, 1.2f), FMath::Abs(ForwardAxis));

	OutForwardAxis = FMath::Clamp(OutForwardAxis, -1.0f, 1.0f);
	OutRightAxis = FMath::Clamp(OutRightAxis, -1.0f, 1.0f);

	return MakeTuple(OutForwardAxis, OutRightAxis);
}

void APCEPlayerCharacter::PlayerMovementInput(bool bIsForwardAxis)
{
	FVector ForwardVector = UKismetMathLibrary::GetForwardVector(FRotator(0.0f, GetControlRotation().Yaw, 0.0f));
	FVector RightVector = UKismetMathLibrary::GetRightVector(FRotator(0.0f, GetControlRotation().Yaw, 0.0f));
	TTuple<float, float> GamepadValues = FixDiagonalGamepadValues(InputComponent->GetAxisValue("MoveForward"), InputComponent->GetAxisValue("MoveRight"));
	float MoveForward = GamepadValues.Get<0>();
	float MoveRight= GamepadValues.Get<1>();

	PCE_LOG_SCREEN_INFO(4, "MoveForward: %f", MoveForward);
	PCE_LOG_SCREEN_INFO(5, "MoveRight: %f", MoveRight);

	if (bIsForwardAxis)
	{
		AddMovementInput(ForwardVector, MoveForward);
	}
	else
	{
		AddMovementInput(RightVector, MoveRight);
	}
}

<cpp 파일>


참고 자료

www.youtube.com/watch?v=FepPWr7YFlY

 

출처 ; https://lykanstudio.tistory.com/25

728x90
반응형