Actor 라이프 사이클

김민수·2025년 1월 21일

언리얼 C++

목록 보기
11/32

1. Actor 라이프 사이클

Actor는 생성 → 초기화 → 실행 → 제거 단계를 거친다.
이 과정에서 언리얼 엔진은 특정 함수를 호출하며, 이를 활용해서 Actor의 상태와 동작을 정의할 수 있다.

1. Actor 생성 단계

(1) 생성자 (Constructor)

  • Actor 객체가 메모리에 생성될 때 한 번 호출된다.
  • 아직 Actor가 월드에 완전히 등록되지 않았으므로, 다른 Actor나 월드 관련 작업은 안전하지 않다.
  • 보통 컴포넌트 생성(CreateDefaultSubobject)과 기본값 설정에 사용한다.
AActor::AActor()
{
    // Tick 활성화 여부 설정
    PrimaryActorTick.bCanEverTick = true;

    // Scene Root 컴포넌트 생성
    SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
    SetRootComponent(SceneRoot);

    // Static Mesh 컴포넌트 생성
    StaticMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
    StaticMeshComp->SetupAttachment(SceneRoot);
}

(2) PostInitializeComponents()

  • Actor의 모든 컴포넌트가 초기화된 직후 호출된다.
  • 컴포넌트 간의 종속성 설정 또는 초기화 작업을 처리하기에 적합한 위치다.
virtual void PostInitializeComponents() override;

void AMyActor::PostInitializeComponents()
{
    Super::PostInitializeComponents();
    UE_LOG(LogTemp, Warning, TEXT("Components initialized!"));
}

2. Actor 시작 단계

(1) OnConstruction(const FTransform& Transform)

  • Actor가 월드에 배치되거나, 블루프린트 속성이 변경될 때 호출된다.
  • 주로 디자인 작업과 연동되며, 에디터에서 Actor의 속성이 변경될 때 자동으로 호출된다.
virtual void OnConstruction(const FTransform& Transform) override;

void AMyActor::OnConstruction(const FTransform& Transform)
{
    Super::OnConstruction(Transform);
    UE_LOG(LogTemp, Warning, TEXT("Actor placed or modified in editor!"));
}

(2) BeginPlay()

  • Actor가 월드에 배치되고 Play 모드가 시작될 때 한 번 호출된다.
  • 이 시점에서는 월드와 다른 Actor들이 준비된 상태이므로, 다른 시스템과의 연동 작업이 가능하다.
  • 초기화 작업 및 게임 로직 설정에 적합하다.
virtual void BeginPlay() override;

void AMyActor::BeginPlay()
{
    Super::BeginPlay();
    UE_LOG(LogTemp, Warning, TEXT("Actor has entered the game world!"));
}

3. Actor 실행 단계

Tick(float DeltaTime)

  • 매 프레임마다 호출되며, Actor의 지속적인 업데이트 작업(이동, 애니메이션, 물리 연산 등)을 처리한다.
  • 불필요한 Actor의 Tick은 비활성화하여 성능을 최적화할 수 있다.
virtual void Tick(float DeltaTime) override;

void AMyActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    UE_LOG(LogTemp, Log, TEXT("Actor is updating every frame!"));
}

4. Actor 종료 단계

(1) EndPlay(const EEndPlayReason::Type EndPlayReason)

  • Actor가 월드에서 더 이상 활동하지 않을 때 호출된다.
  • 호출 이유는 EEndPlayReason으로 전달된다.
    (예: 레벨 종료, Actor 파괴, 게임 종료 등)
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

void AMyActor::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
    Super::EndPlay(EndPlayReason);
    UE_LOG(LogTemp, Warning, TEXT("Actor has been removed from the world."));
}

(2) Destroyed()

  • Destroy()가 호출되어 Actor가 완전히 파괴되기 직전에 호출된다.
  • Actor가 사용하던 리소스 해제 또는 최종 정리 작업을 처리한다.
virtual void Destroyed() override;

void AMyActor::Destroyed()
{
    Super::Destroyed();
    UE_LOG(LogTemp, Error, TEXT("Actor is being destroyed!"));
}

2. Actor 라이프 사이클 호출 순서

  1. Actor 생성
    • Constructor (AActor::AActor())
    • PostInitializeComponents()
  2. Actor가 월드에 추가됨
    • OnConstruction()
    • BeginPlay()
  3. Actor 활성화 (게임 실행 중)
    • Tick(float DeltaTime)
  4. Actor 소멸
    • EndPlay(const EEndPlayReason::Type EndPlayReason)
    • Destroyed()
profile
안녕하세요

0개의 댓글