게임 개발/학습 일지

언리얼 C++ - Actor의 라이프 사이클

이개 2026. 4. 11. 20:26

로그 찍기

툴바의 Window에서 Output Log 체크하기

 

 

LogTemp같은 로그 카테고리 만드는 법

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

#pragma once

#include "CoreMinimal.h" // 언리얼 엔진의 기본 타입
#include "GameFramework/Actor.h" // 액터에 대한 정보를 가지고 있는 헤더파일
#include "Item.generated.h" // 리플렉션 시스템에 관련한 헤더 파일

DECLARE_LOG_CATEGORY_EXTERN(LogSparta, Warning, All);

UCLASS() // 리플렉션 시스템에 등록해주는 키워드
// SPARTAPROJECT_API는 이 클래스를 모듈 밖으로 내보내게 해주는 일종의 매크로다.
// 빌드할 때 중요하다.
// 보통 API 앞에 이름을 붙여 쓴다.
class SPARTAPROJECT_API AItem : public AActor // A 접두어는 Actor계열이라는 의미이다. Object는 U, 구조체는 F, Enum은 E
{
	GENERATED_BODY() // UCLASS와 같은 역할의 매크로
	
public:	
	// Sets default values for this actor's properties
	AItem();

protected:
	USceneComponent* SceneRoot;
	UStaticMeshComponent* StaticMeshComp;

	virtual void BeginPlay() override;
};
// Fill out your copyright notice in the Description page of Project Settings.
  
#include "Item.h"

DEFINE_LOG_CATEGORY(LogSparta);

// Sets default values
AItem::AItem()
{
	SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
	SetRootComponent(SceneRoot);

	StaticMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
	StaticMeshComp->SetupAttachment(SceneRoot);


		static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(TEXT("/Game/Resources/Props/SM_Chair.SM_Chair"));
		if (MeshAsset.Succeeded())
		{
			StaticMeshComp->SetStaticMesh(MeshAsset.Object);
		}

		static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("/Game/Resources/Materials/M_Metal_Steel.M_Metal_Steel"));
		if (MaterialAsset.Succeeded())
		{
			StaticMeshComp->SetMaterial(0, MaterialAsset.Object);
		}
}

void AItem::BeginPlay()
{
	Super::BeginPlay();

	UE_LOG(LogTemp, Warning, TEXT("My Log!"));
	UE_LOG(LogSparta, Error, TEXT("My Sparta!"));
	// UE_LOG(LogTemp, Display, TEXT("My Log!"));
	// UE_LOG(LogTemp, Error, TEXT("My Log!"));
	
}

 

 

Actor의 라이프 사이클

  1. 생성: 메모리에 생김. 1번 호출
  2. PostInitializeComponents() - 컴포넌트가 완성된 직후 호출 컴포넌트끼리 데이터 주고받거나 상호작용
  3. BeginPlay(): 배치(Spawn) 직후
  4. Tick(float DeltaTime): 매 프레임마다 호출
  5. Destroyed(): 삭제되기 직전에 호출
  6. EndPlay(): 게임 종료, 파괴 또는 레벨 전환

확인 코드

// Fill out your copyright notice in the Description page of Project Settings.
  
#include "Item.h"

DEFINE_LOG_CATEGORY(LogSparta);

// Sets default values
AItem::AItem()
{
	SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
	SetRootComponent(SceneRoot);

	StaticMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
	StaticMeshComp->SetupAttachment(SceneRoot);


		static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(TEXT("/Game/Resources/Props/SM_Chair.SM_Chair"));
		if (MeshAsset.Succeeded())
		{
			StaticMeshComp->SetStaticMesh(MeshAsset.Object);
		}

		static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("/Game/Resources/Materials/M_Metal_Steel.M_Metal_Steel"));
		if (MaterialAsset.Succeeded())
		{
			StaticMeshComp->SetMaterial(0, MaterialAsset.Object);
		}

		UE_LOG(LogSparta, Warning, TEXT("%s Constructor"), *GetName())
}

void AItem::PostInitializeComponents()
{
	Super::PostInitializeComponents(); // 부모 생성자 호출 필수
	UE_LOG(LogSparta, Warning, TEXT("%s PostInitializeComponents"), *GetName());
}

void AItem::BeginPlay()
{
	Super::BeginPlay();
	UE_LOG(LogSparta, Warning, TEXT("%s BeginPlay"), *GetName());
}

void AItem::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	// UE_LOG(LogSparta, Warning, TEXT("%s Tick"), *GetName()) // 매 틱 호출은 곤란하다.
}

void AItem::Destroyed()
{
	UE_LOG(LogSparta, Warning, TEXT("%s Destroyed"), *GetName());
	Super::Destroyed();
}

void AItem::EndPlay(const EEndPlayReason::Type EEndPlayReason)
{
	UE_LOG(LogSparta, Warning, TEXT("%s EndPlay"), *GetName());
	Super::EndPlay(EEndPlayReason);
}

 

 

*GetName() 대신 *GetActorLabel() 하면 아웃라이너에 있는 이름을 가져올 수 있음