AActor

Learn how to manage AActor events like BeginPlay and Tick, and manipulate actor properties like location, rotation, and scale in Unreal Engine 5.

Unreal Engine 5 AActor Guide

Understanding AActor in Unreal Engine 5

This guide provides essential information on working with the AActor class in Unreal Engine 5. Actors are the fundamental building blocks of any level in Unreal Engine. They can be anything from a player character, a projectile, a light source, to a simple static mesh. Understanding how to manage their lifecycle and properties is crucial for game development.

Event BeginPlay

The BeginPlay event is a vital part of an Actor's lifecycle. It is called once when the Actor is spawned into the game world, after all of its components have been initialized. This is the ideal place to set up initial game logic, spawn other Actors, or perform any setup that needs to happen at the start of the game or when an Actor enters the world.

event_beginplay

.h File Declaration:

protected:
    virtual void BeginPlay() override;

.cpp File Implementation:

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

    // Your initialization code here...
    UE_LOG(LogTemp, Warning, TEXT("ExampleActor BeginPlay called!"));
}

Event Tick

The Tick event is called every frame, providing a delta time value that represents the time elapsed since the last frame. This is used for continuous updates, animations, physics simulations, and any logic that needs to run every frame. It's important to enable ticking by setting PrimaryActorTick.bCanEverTick to true in the Actor's constructor if you intend to use it.

event_tick

Make sure that PrimaryActorTick.bCanEverTick is set to true in the constructor, so the tick function will be called.

.h File Declaration:

public:
    virtual void Tick(float DeltaTime) override;

.cpp File Implementation:

void AExampleActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    // Your per-frame update code here...
    // Example: Rotate the actor slightly each frame
    // AddActorLocalRotation(FRotator(0.0f, 10.0f * DeltaTime, 0.0f));
}

Manipulating Actor Transform

Actors have a transform, which is composed of their location, rotation, and scale in the world. Unreal Engine provides several functions to modify these properties directly.

Set Actor Location

This function allows you to directly set the world location of an Actor. It takes an FVector representing the new position.

set_actor_location

AActor* ExampleActor; // Assume this is a valid pointer to an Actor
FVector NewLocation = FVector(100.0f, 200.0f, 50.0f);
ExampleActor->SetActorLocation(NewLocation);

Set Actor Rotation

You can set an Actor's rotation using an FRotator or an FQuat. The following example demonstrates setting rotation using an FRotator, which is often more intuitive for pitch, yaw, and roll.

set_actor_rotation

AActor* ExampleActor; // Assume this is a valid pointer to an Actor
FRotator NewRotation = FRotator(0.0f, 90.0f, 0.0f); // Yaw = 90 degrees
ExampleActor->SetActorRotation(NewRotation);

Set Actor Scale 3D

Modify the scale of an Actor using the SetActorScale3D function, which takes an FVector representing the scale along the X, Y, and Z axes.

set_actor_scale_3d

AActor* ExampleActor; // Assume this is a valid pointer to an Actor
FVector NewScale3D = FVector(2.0f, 0.5f, 1.0f); // Double scale on X, half on Y
ExampleActor->SetActorScale3D(NewScale3D);

Set Actor Transform

For convenience, you can set the Actor's location, rotation, and scale all at once using the SetActorTransform function, which takes an FTransform object. An FTransform encapsulates all three properties.

set_actor_transform

AActor* ExampleActor; // Assume this is a valid pointer to an Actor
FVector NewLocation = FVector(0.0f, 0.0f, 100.0f);
FRotator NewRotation = FRotator(45.0f, 0.0f, 0.0f); // Pitch = 45 degrees
FVector NewScale3D = FVector(1.0f, 1.0f, 1.0f);

// Create FTransform (rotation, location and scale)
FTransform NewTransform(NewRotation, NewLocation, NewScale3D);

ExampleActor->SetActorTransform(NewTransform);

Controlling Actor Visibility

Set Actor Hidden In Game

This function allows you to control whether an Actor is rendered in the game. Setting it to true will hide the Actor, and setting it to false will make it visible again.

set_actor_hidden_in_game

AActor* ExampleActor; // Assume this is a valid pointer to an Actor
bool bHideActor = true; // Set to true to hide, false to show
ExampleActor->SetActorHiddenInGame(bHideActor);

Destroying Actors

Destroy Actor

When an Actor is no longer needed, it should be destroyed to free up memory and resources. The Destroy() function handles this process. It's important to note that after calling Destroy(), the Actor pointer becomes invalid.

destroy_actor

AActor* ExampleActor; // Assume this is a valid pointer to an Actor
if (ExampleActor)
{
    ExampleActor->Destroy();
    ExampleActor = nullptr; // Good practice to nullify the pointer after destruction
}

Further Learning Resources