Unreal C++/Custom Actor

C++ Custom Actor creation in Blueprint

cwisky 2017. 11. 23. 21:04

C++으로 작성한 커스텀 액터 클래스를 블루프린트에서 사용하기


Actor 클래스를 상속하여 C++에서 커스텀 액터 클래스를 생성하면 블루프린트에서는 SpawnActorFromClass 노드를 사용하여 일반 블루프린트로 만든 액터와 다를 바 없이 생성하여 사용할 수가 있다


언리얼 엔진을 실행하고 C++ 프로젝트를 생성한다


Content Browser 에서 마우스 우측 > 새 C++ 클래스 > 부모 클래스 선택 창에서 Actor 를 선택하고 [다음] 버튼을 누른다


파일 저장 경로를 선택하는 창에서 [클래스 생성] 버튼을 누르면 클래스가 생성되어 언리얼에 컴파일 되고 이어서 Visual Studio 가 실행되면서 생성된 클래스 헤더파일과 소스파일이 편집가능한 상태가 된다


필요한 속성이나 함수를 추가하고 정의한다


헤더파일과 소스파일을 저장하고 다시 언리얼 에디터의 툴바에서 [컴파일] 버튼을 눌러 컴파일한다


블루프린트에서 Spawn Actor from Class 노드를 추가하고 Class 핀에 커스텀 액터클래스의 이름을 지정하면 된다. 블루프린트에서 작성한 블루프린트 Actor 클래스를 사용하는 방법과 다를 바가 없다


생성된 C++액터클래스의 헤더파일 (적색으로 표시한 내용은 추가된 부분임)

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


#pragma once


#include "CoreMinimal.h"

#include "GameFramework/Actor.h"

#include "MyActor.generated.h"


UCLASS()

class BP_CPP_API AMyActor : public AActor

{

GENERATED_BODY()

public:

// Sets default values for this actor's properties

AMyActor();


protected:

// Called when the game starts or when spawned

virtual void BeginPlay() override;


public:

// Called every frame

virtual void Tick(float DeltaTime) override;


UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="MyActor")

FString message;


UFUNCTION(BlueprintCallable, Category="MyActor")

void SetMessage(FString msg);


};



생성된 C++ 액터클래스의 소스파일(적색으로 표시한 내용은 추가된 부분임)

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


#include "MyActor.h"



// Sets default values

AMyActor::AMyActor()

{

  // 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 AMyActor::BeginPlay()

{

Super::BeginPlay();

}


// Called every frame

void AMyActor::Tick(float DeltaTime)

{

Super::Tick(DeltaTime);


}


void AMyActor::SetMessage(FString msg)

{

this->message = msg;

}



블루프린트에서 C++ 커스텀 액터를 생성(Spawn)하고 사용하는 예