본문 바로가기
Unreal Engine/Unreal 기초

UnrealEnigne Cpp함수를 블루프린트에서 사용

by k99812 2025. 2. 7.

Cpp함수를 블루프린트에서 사용

 

Cpp함수를 블루프린트에서 사용하려면 Cpp함수에 UFUNCTION 매크로와 매크로안에 추가적인 키워드를 넣어야 한다

키워드는 총 3가지로 BlueprintCallable, BlueprintImplementableEvent, BlueprintNativeEvent 가 있다.

 

각 키워드별 특징

 

BlueprintCallable 

BlueprintCallable 은 일반적인 Cpp 함수에 UFUNCTION(BlueprintCallable)를 붙이는 것으로 사용할 수 있다.

옵션의 이름처럼 블루프린트에서는 해당 함수를 호출하는 것만 가능하고 해당 함수의 구현부를 구현할 수 없다.

 

//UPPGameOverUserWidget.h
UFUNCTION(BlueprintCallable, Meta = (DisplayName = "BtnEventGameRestartCpp"))
void BtnEventGameRestart();

//UPPGameOverUserWidget.cpp
void UPPGameOverUserWidget::BtnEventGameRestart()
{
	UE_LOG(LogTemp, Log, TEXT("OnCliked BtnEventGameRestart"));
}

 

BlueprintImplementableEvent

BlueprintImplementableEvent 는 BlueprintCallable 과 반대로 Cpp에서는 선언만 할 수 있고 구현부는 구현할 수 없다

그래서 블루프린트에서 해당 함수를 구현 해야한다.

언리얼 엔진에서는 해당 키워드를 가진 함수에 네이밍 규칙으로 접두사 "K2_"를 붙인다(K2_함수명)

비주얼 스튜디오에서 구현부를 만들지 않아 오류가 뜨지만 빌드시 문제가 없다

UFUNCTION(BlueprintImplementableEvent, Category = "Game", Meta = (DisplayName = "OnGameOverCpp"))
void K2_OnGameOver();

 

BlueprintNativeEvent 

BlueprintNativeEvent 는 위에 두 옵션을 합친 느낌으로 Cpp에서 선언과 구현을 할 수 있고 블루프린트에서도 구현부를 수정 및 추가할 수있다.

해당 키워드를 붙인 함수를 오버라이드 하거나 Cpp에서는 함수명 뒤에 "_Implementation"를 붙여야 된다

 

//헤더파일
virtual FString GetNotifyName_Implementation() const override;

//cpp파일
FString UAnimNotify_GASAttackHitCheck::GetNotifyName_Implementation() const
{
	return FString(TEXT("GASAttackHitCheck"));
}

 

 

추가적으로

UFUNCTION 매크로 안에 Meta = ("표시할이름") 으로 블루프린트에서 표시되는 이름을 변경할 수 있다.

또 Category를 지정할 수 있다