Actor C++로 만들어보기

Ryan Ham·2024년 6월 21일
1

Unreal Engine

목록 보기
12/27
// Actor.h

	// TObjectPtr로 FountainMesh랑 WaterMesh를 선언해준다. 
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category=Mesh)
	TObjectPtr<UStaticMeshComponent>  FountainMesh;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	TObjectPtr<UStaticMeshComponent>  WaterMesh;

// Actor.cpp
#include "Asset/Fountain.h"
#include "Components/StaticMeshComponent.h"

// Sets default values
AFountain::AFountain()
{
 	// 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;

	// 
	FountainMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Body"));
	WaterMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Water"));

	// 계층구조 성립
	RootComponent = FountainMesh;
	WaterMesh->SetupAttachment(FountainMesh);
	WaterMesh->SetRelativeLocation(FVector(0.f, 0.f, 140.f));

	// ConstructorHelpers로 static mesh reference 불러오기
    // 여기서 하나의 꿀팁이 있는데 Scope 연산자를 적극 활용해서 Asset이라는 이름을 중복해서 사용해보자. 
	{
		static ConstructorHelpers::FObjectFinder<UStaticMesh> Asset(TEXT("/Script/Engine.StaticMesh'/Game/ArenaBattle/Environment/Props/SM_Plains_Castle_Fountain_01.SM_Plains_Castle_Fountain_01'"));
		if (Asset.Object) {
			FountainMesh->SetStaticMesh(Asset.Object);
		}
	}

	{
		static ConstructorHelpers::FObjectFinder<UStaticMesh> Asset(TEXT("/Script/Engine.StaticMesh'/Game/ArenaBattle/Environment/Props/SM_Plains_Fountain_02.SM_Plains_Fountain_02'"));
		if (Asset.Object) {
			WaterMesh->SetStaticMesh(Asset.Object);
		}
	}
}

Actor와 Component의 개념

Actor는 transform을 가지며 world으로부터 tick과 시간 서비스를 받는다.
Actor는 root component를 가져야 하며, root component의 transform은 actor의 transform을 의미한다.

BP를 만들면 DefaultSceneRoot하는 root component가 자동으로 생성되게 된다.
BP에서 static mesh들을 계층구조로 만들어서 상대 tranfrom값을 가지도록 만들 수 있다.

  1. BP로 Actor 만들어보기
  2. C++로 Actor 만들어보기
  • C++ Actor에서 Component의 생성

    1. Component는 Unreal Object이므로 Actor.h 파일에서 UPROPERTY macro를 설정하고 TObjectPtr로 pointer를 선언.

    2. Component의 등록
      - CDO에서 생성한 component는 자동으로 world에 등록된다.
      - CDO가 아닌 runtime에서 NewObject로 생성한 component는 반드시 등록절차를 거쳐야 한다. 예) RegisterComponent
      - 등록된 component는 world의 기능을 사용할 수 있으며, physic과 rendering 처리에 합류한다.

    3. Component의 확장 설계
      - UPROPERTY에 specifier를 설정할 수 있음.

    4. Component 지정자
      - Visible / Edit
      - Anywhere / DefaultsOnly / InstanceOnly
      - BlueprintReadOnly / BlueprintReadWrite
      - Category

반대로, 이렇게 만든 C++ 클래스를 Unreal Editor에서 BP를 만들고 상속 받을 수 있다.
1. Unreal Editor에서 BP 만들기
2. BP -> 상단의 class setting -> class option -> 부모 클래스 지정


Pawn이란?

Actor를 상속 받은 class. Player가 possess 가능하다.

  • Pawn은 기본적으로 3가지 주요 component으로 이루어져 있다.
    • Gimmick과 상호작용을 담당하는 collision component(Root component)
    • 시각적인 비주얼을 담당하는 mesh component
    • 움직임을 담당하는 movement component

Component 중에서 transform 없이 기능만 제공하는 component를 actor component라고 한다. Transform이 있는 component를 scene component라고 한다.

  • 캐릭터는 언리얼이 제공하는 전문 Pawn Class
    • Capsule Component(Root Component)
    • Skeletal Mesh Component
    • CharacterMovement Component

카메라

기본적인 카메라 setting을 할때 CameraCamera Arm 을 같이 배치한다. Camera Arm은 마치 현실에서의 셀카봉이라고 이해하면 쉬운데, 카메라와 연결된 긴 장치가 있어 이를 움직이면 Arm 끝 부분에 달린 카메라 또한 같이 움직이는 원리이다.

Camera를 Camera Arm에 설치하는 방법은 아래와 같다.

FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName)

여기서 USpringArmComponent::SocketName은 USpringArmComponent class에 정의되어 있는 constant이고, spring arm에 components들을 부착할 수 있는 default socket을 의미한다.


Reference

  • 이득우 Unreal Programming Section 1-1. Unreal Engine Game 제작 기초
  • 이득우 Unreal Programming Section 1-1. Character & Input system
profile
🏦KAIST EE | 🏦SNU AI(빅데이터 핀테크 전문가 과정) | 📙CryptoHipsters 저자

0개의 댓글