C++ 클래스를 이용하여 액터를 생성하고 Z 위치를 변경하는 예
https://www.youtube.com/watch?v=K8iSi1oGaBI
C++ 프로젝트 생성
C++ 클래스 생성(Actor 클래스 기반, 클래스이름: CubeActor 등으로 지정)
Content Browser 안에 C++ 클래스가 보이지 않으면 우측 하단의 [뷰옵션] > [C++ 클래스 표시] 선택
VS에 CubeActor.h, CubeActor.cpp 파일이 생성된다
CubeActor.cpp의 Tick 함수에 아래의 적색부분 같이 작성하여 게임 시작시에 큐브가 상승하도록 한다
// Fill out your copyright notice in the Description page of Project Settings.
#include "CubeActor.h"
// Sets default values
ACubeActor::ACubeActor()
{
// 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 ACubeActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACubeActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector NewLoc = GetActorLocation();
NewLoc.Z += 5;
SetActorLocation(NewLoc);
}
위와 같이 cpp 파일을 편집하여 VS에서 저장하고 UE4의 툴바에서 [컴파일] 버튼을 누른다
컴파일이 완료되면 컨텐츠 브라우저에서 CubeActor를 드래그하여 뷰포트에 올릴 수 있지만 아무런 형제가 없기에 뷰포트에서 선택할 수도 없는 상태가 된다
이와같이 C++ 액터클래스 드래그하여 뷰포트에서 보여지고 또 마우스로 선택이 가능하도록 하려면 가시적인 컴포넌트 한개는 있어야 한다.
C++액터 클래스에 컴포넌트를 포함시키고자 한다면 다음과 같은 코드를 h, cpp 파일에 추가한다(적색부분 참조)
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CubeActor.generated.h"
UCLASS()
class CPP_CUBE_API ACubeActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACubeActor();
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)
UStaticMeshComponent* Mesh;
};
cpp 코드에 다음과 같이 액터에 컴포넌트를 등록하는 부분을 작성한다
// Fill out your copyright notice in the Description page of Project Settings.
#include "CubeActor.h"
// Sets default values
ACubeActor::ACubeActor()
{
// 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;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>("MyMesh");
}
// Called when the game starts or when spawned
void ACubeActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACubeActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector NewLoc = GetActorLocation();
NewLoc.Z += 5;
SetActorLocation(NewLoc);
}
위와같이 작성이 되었으면 VS에서 저장하고 UE4에서 [컴파일] 버튼을 누르고 컴파일을 완료한다
이제 컨텐츠 브라우저에서 CubeActor 를 드래그하여 뷰포트에 올리면 뷰포트에 형체는 없지만 방향표시 기즈모가 나타나기 때문에 [디테일] 뷰에서 Mesh 변수에 구체적인 스테틱 메시를 지정해 줄 수 있게 된다