언리얼 엔진에서 C++ 클래스는 Blueprint 클래스 안에서 변수로 선언될 수도 있고 블루프르린트 클래스의 부모 클래스가 될 수 있다.
아래처럼 UCLASS 매크로를 선언해주면 된다
UCLASS (BlueprintType) // BP안에 선언되는 변수의 자료형(Type)이 될 수 있도록 선언
UCLASS Blueprintable) // BP 클래스를 생성할 때 부모 클래스가 될 수 있도록 선언
UCLASS(Blueprintable, BlueprintType)
#include "Object.h"
#include "PlayerClass.generated.h"
UCLASS(BlueprintType)
class PROTOTYPE2_API UPlayerClass : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Variables")
FString playerName;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Variables")
FString playerTeam;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Variables")
FString picPath;
UPlayerClass(const FObjectInitializer& ObjectInitializer);
UFUNCTION(BlueprintCallable, Category = "Import")
virtual void ImportPic(const FString& Path);
};
언리얼 엔진에서는 모든 식별자에 Pascal Case(첫자를 대문자로) 사용
언리얼 클래스 Prefix
U - UObject 하위 클래스 예) UTexture
A - AActor 하위 클래스 예) AGameMode
F - 이외의 모든 클래스와 struct 들 예) FName, FVector
T - 템플릿 예) TArray, TMap
I - 인터페이스 클래스 예) ITransaction
E - Enum 타입 예) ESelectionMode
b - Boolean 변수 예) bEnabled
C++ 변수가 BP의 Set, Get 노드 지원되도록 하려면
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Damage")
int32 TotalDamage;
C++ 클래스를 BP 안에서 인스턴스화하고 함수를 호출하는 예
Blueprint 프로젝트 안에서 Object 클래스를 기반으로 C++클래스를 아래처럼 생성한다
MyObject.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyObject.generated.h"
/**
*
*/
UCLASS(BlueprintType)
class CPP_IN_BPPROJ_API UMyObject : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Test")
int Score;
UFUNCTION(BlueprintCallable, Category="Test")
int Add(int a, int b);
UFUNCTION(BlueprintCallable, Category = "Test")
static void Create(UClass *ObjectClass, UMyObject* &CreatedObject);
};
MyObject.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyObject.h"
int UMyObject::Add(int a, int b)
{
return a+b;
}
void UMyObject::Create(UClass * ObjectClass, UMyObject *& CreatedObject)
{
CreatedObject = NewObject<UMyObject>((UMyObject*)GetTransientPackage(), ObjectClass);
}
Blueprint 클래스에서 다음과 같이 MyObject 클래스의 인스턴스를 생성하고 사용할 수 있다