본문 바로가기
개발/Unreal

언리얼 5 BP - C++로 스태틱 함수를 만들어서 블루프린트에 사용하기 (Create C++ Static Function and Access From Blueprint)

by 피로물든딸기 2023. 6. 19.
반응형

Unreal 5 전체 링크

 

아래와 같이 정수를 더하는 함수가 있다고 가정하자.

int MyAdd(int a, int b)
{
	return a + b;
}

 

블루프린트로 나타내면 아래와 같을 것이다.

 

만약 개발을 하다가 아래와 같은 복잡한 계산이 필요하다고 가정하자.

int MyCalculate(int a, int b)
{
	return a * b + b * 2 + a / b - 3 * a;
}

 

함수로 표현하면 간단하지만, 노드로 표현하면 보기도 힘들고, 검증하기도 힘들다.

 

이런 경우는 스태틱 함수를 만들어서 블루프린트 내에 언제 어디서든 사용하게 만드는 것이 좋다.

 

C++에서 정적 함수를 만들면 인스턴스를 생성하지 않고 클래스 이름으로만 접근이 가능하다.

수학이나 계산과 관련된 함수는 static으로 만들어두면 편리하다.


C++ 프로젝트 생성

 

위의 static 메서드를 만들기 위해 C++ 프로젝트를 만든다.

 

[툴] → [새로운 C++ 클래스...]를 선택한다.

 

클래스는 액터로 추가하였다. 

 

이름을 MyStaticMathActor로 정하고 클래스를 생성한다.

 

컴파일을 완료하면 MyStaticMathActor가 만들어진다.


MyStaticMathActor 수정하기

 

MyStaticMathActor.h에는 static 함수를 선언한다.

그리고 블루프린트에 사용할 수 있도록 BlueprintCallable 지정자를 이용한다.

카테고리를 설정해야 제대로 컴파일이 된다.

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION(BlueprintCallable, Category = "Math")
		static int MyCalculate(int a, int b);
};

 

그리고 MyStaticMathActor.cpp에는 필요한 함수를 구현한다.

이때, MyStaticMathActorActor이기 때문에 A를 앞에 붙여야 한다. (AMyStaticMathActor::)

int AMyStaticMathActor::MyCalculate(int a, int b)
{
	return a * b + b * 2 + a / b - 3 * a;
}

 

코드를 모두 작성하였으면 다시 컴파일을 하자.

 

전체 코드는 다음과 같다.

 

MyStaticMathActor.h

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyStaticMathActor.generated.h"

UCLASS()
class BPCPLUSPLUS_API AMyStaticMathActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyStaticMathActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION(BlueprintCallable, Category = "Math")
		static int MyCalculate(int a, int b);
};

 

MyStaticMathActor.cpp

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


#include "MyStaticMathActor.h"

// Sets default values
AMyStaticMathActor::AMyStaticMathActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
}

// Called when the game starts or when spawned
void AMyStaticMathActor::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AMyStaticMathActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

int AMyStaticMathActor::MyCalculate(int a, int b)
{
	return a * b + b * 2 + a / b - 3 * a;
}

블루프린트에서 스태틱 함수 사용하기

 

아웃라이너에서 BP_RiflePickUp블루프린트 편집을 클릭한다.

 

이벤트그래프에서 마우스 오른쪽 버튼을 클릭하면 Math 카테고리에 MyCalculate가 만들어진 것을 알 수 있다.

 

제대로 MyCalculate가 동작하는지 확인하기 위해, A = 50 / B = 10을 설정하자.

그리고 무기를 집었을 때, Print String으로 출력해보자.

 

MyCalculate(50, 10) = 50 * 10 + 10 * 2 + 50 / 10 - 3 * 50 = 500 + 20 + 5 - 150 = 375가 된다.

실제로 뷰포트에서 375가 잘 출력되는 것을 알 수 있다.


주의 사항

 

만약 클래스를 만들 때, 액터가 아니라 블루프린트 함수 라이브러리로 만들었다고 가정하자.

 

그러면 아래의 파일이 생성된다.

MyBlueprintFunctionLibrary.h

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

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

/**
 * 
 */
UCLASS()
class BPCPLUSPLUS_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
};

 

MyBlueprintFunctionLibrary.cpp

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


#include "MyBlueprintFunctionLibrary.h"

 

UBlueprintFunctionLibrary를 상속하기 때문에 static 함수를 만들 때, U를 붙여야 한다. 

(액터에서는 A를 앞에 추가했었다.)

class BPCPLUSPLUS_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary

 

즉, cpp에서는 아래의 함수를 구현하게 된다. (UMyBlueprintFunctionLibrary::)

int UMyBlueprintFunctionLibrary::MyCalculate(int a, int b)

 

구현된 코드를 참고하자.

 

MyBlueprintFunctionLibrary.h

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

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

/**
 * 
 */
UCLASS()
class BPCPLUSPLUS_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
public:
	UFUNCTION(BlueprintCallable, Category = "Math")
		static int MyCalculate(int a, int b);
};

 

MyBlueprintFunctionLibrary.cpp

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


#include "MyBlueprintFunctionLibrary.h"

int UMyBlueprintFunctionLibrary::MyCalculate(int a, int b)
{
	return a * b + b * 2 + a / b - 3 * a;
}
반응형

댓글