Once your projects begin to increase in scale, you may find it difficult to locate an individual Actor in your scene. It is common to have hundreds of Actors in your scene ranging from level set pieces and geometry, to NPCs, enemies, interactable objects, and pick-ups. When working in the Unreal Editor, You can use the World Outliner to assist in finding an Actor in your scene.
With an Actor selected in the World Outliner, and while navigating inside the Level Viewport, double-clicking the Actor or pressing the F key will move the camera to the location of the Actor selected to Focus on it.
Refer to the World Outliner documentation for more information on locating an Actor in your scenes.
Project Setup
In this tutorial, you will find Actors in a Level by using the Get All Actors of Class node. When this node is called it will retrieve all Actors in your Level of the specified Class and place them in an Array. Then, from that Array, you can retrieve an Actor or Actors based on your filter criteria. You can then access the properties of the Actor(s) or modify them in some way based on the functionality you want to achieve.
- Begin by creating a New > Games > ThirdPerson > Blueprint Project with Starter Content, named FindingActors.
- Navigate to Edit > Project Settings > Engine > Input > Bindings > Action Mappings, then click Add(+) to create a new action mapping named FindActorPressed, then set the key value to 1.
In this tutorial, you will find Actors in a Level by using the Gameplay Statics Class Library (Get All Actors of Class node). When this Function (node) is called, it will retrieve all Actors in your Level of the specified Class and place them in an Array. Then, from that Array, you can retrieve all Actors or an Actor based on any filter criteria. You can then access the properties of the Actor(s) or modify them in some way based on the functionality you want to achieve.
- Begin by creating a New > Games > ThirdPerson > C++ Project with Starter Content named FindingActors.
- Navigate to the Edit > Project Settings > Engine > Input > Bindings > Action Mappings, then click Add(+) to create a new action mapping named FindActorPressed, then set the key value to 1.
Creating the FireEffect Actor
The Starter Content folder provides a completed FireEffect Actor that includes a Particle Component to represent the flame effect, and an Audio Component for the sound effects. This Actor will be used as the base Actor class to find from the Get All Actors of Class node.
Navigate to Content > StarterContent > Blueprints, then drag an instance of Blueprint_Effect_Fire into the World.
- Click the Add button to create a New C++ Class then from the Choose a Parent Class menu, select Actor, click Next.
- Name your new Actor class FireEffect and click Create Class.
- In
FireEffect.h
, declare the following:public: //Get the fire sound effect Audio Component. UAudioComponent* GetFireAudioComponent() const { return FireAudioComponent; } //Get the fire effects Particle System Component. UParticleSystemComponent* GetParticleFireComponent() const { return ParticleFireComponent; } protected: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UParticleSystemComponent* ParticleFireComponent; UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UAudioComponent* FireAudioComponent;
- Navigate to the
FireEffect.cpp
file, then include the following class libraries:#include "Particles/ParticleSystemComponent.h" #include "Components/AudioComponent.h"
- In the
AFireEffect
constructor, implement the following code:AFireEffect::AFireEffect() { // 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; ParticleFireComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("P_Fire")); FireAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Fire Audio")); ParticleFireComponent->SetupAttachment(RootComponent); FireAudioComponent->AttachToComponent(ParticleFireComponent,FAttachmentTransformRules::KeepRelativeTransform); }
-
Compile your code.
-
Navigate to Content Browser > C++ Classes > FindingActors and right-click the FireEffect Actor to Create a Blueprint class based on FireEffect named BP_FireEffect.
-
In the Class Defaults of BP_FireEffect, click Particle Fire in the Components panel, then navigate to the Details panel, then under the Particles category open the Template dropdown menu and choose P_Fire.
-
Click Fire Audio in the Components panel, then navigate to the Details panel. From the Sound category, open the Sound variable dropdown menu and choose Fire01_Cue.
-
Click Compile and Save.
Finished Code
FireEffectActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FireEffect.generated.h"
UCLASS()
class FINDINGACTOR_API AFireEffect : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFireEffect();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
public:
//Get the fire sound effect Audio Component.
UAudioComponent* GetFireAudioComponent() const { return FireAudioComponent; }
//Get the fire effects Particle System Component.
UParticleSystemComponent* GetParticleFireComponent() const { return ParticleFireComponent; }
protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UParticleSystemComponent* ParticleFireComponent;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UAudioComponent* FireAudioComponent;
};
FireEffectActor.cpp
#include "FireEffect.h"
#include "Particles/ParticleSystemComponent.h"
#include "Components/AudioComponent.h"
// Sets default values
AFireEffect::AFireEffect()
{
// 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;
// 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;
ParticleFireComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("P_Fire"));
FireAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Fire Audio"));
ParticleFireComponent->SetupAttachment(RootComponent);
FireAudioComponent->AttachToComponent(ParticleFireComponent, FAttachmentTransformRules::KeepRelativeTransform);
}
// Called when the game starts or when spawned
void AFireEffect::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFireEffect::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
Third Person Character: Setting Up the Finding Actor Pressed Event
-
Navigate to the Content > ThirdPerson > Blueprints folder and double click the BP_ThirdPersonCharacter to open its Class Defaults.
-
Right-click on the Event Graph, then from the Actions menu search for the FindActorPressed action key event.
- From the FindActorPressed node Key Event, drag off from the Pressed pin, then from the Actions menu, search for the Get All Actors Of Class node.
-
Inside the Get All Actors of Class node, click the Actor Class, then from the drop-down select the Blueprint_Effect_Fire class.
-
Drag off from the Out Actors pin and in the Actions menu, search for For Each Loop.
- Drag off from the Execution pin of the Get All Actors Of Class node and plug it into the Exec pin of the For Each Loop node.
- From the ForEachLoop node, drag off the Array Element pin, then in the Actions menu, search for Get P Fire.
- Drag off from the Array Element pin, then in the Actions menu, search for Get Fire Audio.
- Drag off from the P Fire pin, then in the Actions menu search for the Deactivate node.
- Drag off from the Fire Audio pin and connect it to the Target pin from the Deactivate node.
-
Click Compile and Save.
-
From the Content Browser, navigate to C++ Classes > FindingActors and double-click FindingActorsCharacter to open the
FindingActorsCharacter.h
file. - In the
FindingActorsCharacter
class defaults, declare the following:protected: void OnFindActorPressed();
- Navigate to
FindingActorsCharacter.cpp
and include the following class libraries:#include "Kismet/GameplayStatics.h" #include "FireEffect.h" #include "Particles/ParticleSystemComponent.h" #include "Components/AudioComponent.h"
Unreal Engine uses an Include-What-You-Use (IWYU) dependency model. This means that the Engine's source code only includes the dependencies that it needs to compile. See IWYU for additional documentation.
- Implement the logic for your
OnFindActorPressed
by declaring the following code:void AFindingActorsCharacter::OnFindActorPressed() { TArray<AActor*> ActorsToFind; if(UWorld* World = GetWorld()) { UGameplayStatics::GetAllActorsOfClass(GetWorld(), AFireEffect::StaticClass(), ActorsToFind); } for (AActor* FireEffectActor: ActorsToFind) { //Is this Actor of type FireEffect class? AFireEffect* FireEffectCast = Cast<AFireEffect>(FireEffectActor); if (FireEffectCast) { //Get the Particle Fire Component and deactivate it. FireEffectCast->GetParticleFireComponent()->Deactivate(); //Get the Fire Audio Component and deactivate it. FireEffectCast->GetFireAudioComponent()->Deactivate(); } } }
- Navigate to the
SetupPlayerInputComponent
method and declare the following code:void AFindingActorsCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) { PlayerInputComponent->BindAction("FindActorPressed", IE_Pressed, this, &AFindingActorsCharacter::OnFindActorPressed); }
- Compile your code.
Finished Blueprint
Finished Code
FindingActorsCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "FindingActorsCharacter.generated.h"
UCLASS(config=Game)
class AFindingActorsCharacter : public ACharacter
{
GENERATED_BODY()
/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
public:
AFindingActorsCharacter();
/** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Input)
float TurnRateGamepad;
protected:
/** Called for forwards/backward input */
void MoveForward(float Value);
/** Called for side to side input */
void MoveRight(float Value);
/**
* Called via input to turn at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void TurnAtRate(float Rate);
/**
* Called via input to turn look up/down at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void LookUpAtRate(float Rate);
/** Handler for when a touch input begins. */
void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location);
/** Handler for when a touch input stops. */
void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location);
protected:
// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// End of APawn interface
public:
/** Returns CameraBoom subobject **/
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
/** Returns FollowCamera subobject **/
FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
protected:
/** Called when Input key is pressed from the Player input component */
void OnFindActorPressed();
};
FindingActorsCharacter.cpp
#include "FindingActorsCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "Kismet/GameplayStatics.h"
#include "FireEffect.h"
#include "Particles/ParticleSystemComponent.h"
#include "Components/AudioComponent.h"
//////////////////////////////////////////////////////////////////////////
// AFindingActorsCharacter
AFindingActorsCharacter::AFindingActorsCharacter()
{
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
// set our turn rate for input
TurnRateGamepad = 50.f;
// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Configure character movement
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate
// Note: For faster iteration times these variables, and many more, can be tweaked in the Character Blueprint
// instead of recompiling to adjust them
GetCharacterMovement()->JumpZVelocity = 700.f;
GetCharacterMovement()->AirControl = 0.35f;
GetCharacterMovement()->MaxWalkSpeed = 500.f;
GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;
// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 400.0f; // The camera follows at this distance behind the character
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
// Create a follow camera
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
// are set in the derived blueprint asset named ThirdPersonCharacter (to avoid direct content references in C++)
}
//////////////////////////////////////////////////////////////////////////
// Input
void AFindingActorsCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
// Set up gameplay key bindings
check(PlayerInputComponent);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
PlayerInputComponent->BindAxis("Move Forward / Backward", this, &AFindingActorsCharacter::MoveForward);
PlayerInputComponent->BindAxis("Move Right / Left", this, &AFindingActorsCharacter::MoveRight);
// We have 2 versions of the rotation bindings to handle different kinds of devices differently
// "turn" handles devices that provide an absolute delta, such as a mouse.
// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
PlayerInputComponent->BindAxis("Turn Right / Left Mouse", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("Turn Right / Left Gamepad", this, &AFindingActorsCharacter::TurnAtRate);
PlayerInputComponent->BindAxis("Look Up / Down Mouse", this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAxis("Look Up / Down Gamepad", this, &AFindingActorsCharacter::LookUpAtRate);
// handle touch devices
PlayerInputComponent->BindTouch(IE_Pressed, this, &AFindingActorsCharacter::TouchStarted);
PlayerInputComponent->BindTouch(IE_Released, this, &AFindingActorsCharacter::TouchStopped);
PlayerInputComponent->BindAction("FindActorPressed", IE_Pressed, this, &AFindingActorsCharacter::OnFindActorPressed);
}
void AFindingActorsCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
{
Jump();
}
void AFindingActorsCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
{
StopJumping();
}
void AFindingActorsCharacter::TurnAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerYawInput(Rate * TurnRateGamepad * GetWorld()->GetDeltaSeconds());
}
void AFindingActorsCharacter::LookUpAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerPitchInput(Rate * TurnRateGamepad * GetWorld()->GetDeltaSeconds());
}
void AFindingActorsCharacter::MoveForward(float Value)
{
if ((Controller != nullptr) && (Value != 0.0f))
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
}
void AFindingActorsCharacter::MoveRight(float Value)
{
if ( (Controller != nullptr) && (Value != 0.0f) )
{
// find out which way is right
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get right vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement in that direction
AddMovementInput(Direction, Value);
}
}
void AFindingActorsCharacter::OnFindActorPressed()
{
TArray<AActor*> ActorsToFind;
if (UWorld* World = GetWorld())
{
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AFireEffect::StaticClass(), ActorsToFind);
}
for (AActor* FireEffectActor : ActorsToFind)
{
//Is this Actor of type FireEffect class?
AFireEffect* FireEffectCast = Cast<AFireEffect>(FireEffectActor);
if (FireEffectCast)
{
//Get the Particle Fire Component and deactivate it.
FireEffectCast->GetParticleFireComponent()->Deactivate();
//Get the Fire Audio Component and deactivate it.
FireEffectCast->GetFireAudioComponent()->Deactivate();
}
}
}
End Result
Navigate to the Toolbar and click Play (PIE).
When pressing the numeric 1 key, you should notice the Fire effect particles will extinguish and the sound effects emitting from the audio component will no longer be heard.
- Navigate to Content Browser, and drag a few instances of your BP_FireEffect into the Level.
-
Navigate to the Toolbar and click Play (PIE).
-
When pressing the numeric 1 key, you should notice the Fire effect particles will extinguish and the sound effects emitting from the audio component will no longer be heard.
Get Specific Actors with Tag
You used the Get All Actors of Class node to get an Array of Actors of a specified Class. However, you can also filter the Array results based on different criteria to get specific Actors or a single Actor from the Array by using Tags on the Actor.
-
Navigate to the Content > StarterContent > Blueprints and select Blueprint_Effect_Fire.
-
In the Details panel, navigate to the Tags section and click Add(+) to add a tag to the Actor.
-
In the 0 element field, enter "FindActorTag" into the text string.
-
Inside the MyCharacter Blueprint, drag off from the Array Element pin of the For Each Loop and in the Actions menu search for Get Tags.
- Drag off from the Tags pin, then in the Actions menu search for the Get(a copy) node.
- Your Blueprint Script should look as following:
- Drag off from the Execution pin of the For Each Loop node, then in the Actions menu search for the Branch node.
- Drag off from the Condition pin of the Branch node and add an Actor Has Tag node.
-
Connect the out pin from the Get node to the Tag pin on the Actor Has Tag node.
-
Connect the Array Element pin of the For Each Loop node to the Target pin on the Actor Has Tag node. Your Blueprint Script should look as following:
-
Connect the True pin of the Branch node with Exec Pin of the Deactivate node.
-
Your Blueprint Script should look as following:
-
Click Compile and Save.
-
Select one of your Blueprint_FireEffect's from the Outliner, then from the Details panel navigate to the Tags variable, locate the FindActorTag field, open the drop-down menu, then select Delete to delete the tag variable.
We are intentionally leaving one of the fire effects without a tag in order to demonstrate the functionality.
-
Navigate to the Toolbar and Press Play (PIE).
You used the GetAllActorsofClass
function from the Gameplay Statics Library, to get an Array of Actors of a specified Class. However, you can also filter the Array results based on different criteria to get specific Actors or a single Actor from the Array by using Tags on the Actor.
-
Begin by navigating to your Content Browser > C++ Classes folder, and double click your FireEffect Actor to open it's
FireEffect.cpp
file. -
Inside the FireEffect constructor add the following code
AFireEffect::AFireEffect() { Tags.Add(FName("FindActorTag")); }
Tags is an array that is a part of the Actor's class defaults, It can be used for grouping and categorizing.
-
Compile your code.
-
Open your BP_FireEffect, then in the Details panel, navigate to the Actor category and you will notice your Actor Tag has been created.
-
Navigate to your Content Browser > C++ Classes Folder, and double-click your FindingActorsCharacter to open its
FindingActorsCharacter.cpp
file. - In the
OnFindActorPressed
method implement the following:void AFindingActorsCharacter::OnFindActorPressed() { TArray<AActor*> ActorsToFind; //Gets all Actors of FireEffect class that have the "FindActorTag" UGameplayStatics::GetAllActorsOfClassWithTag(GetWorld(), AFireEffect::StaticClass(), FName("FindActorTag"),ActorsToFind); for (AActor* FireEffectActor: ActorsToFind) { AFireEffect* FireEffectCast = Cast<AFireEffect>(FireEffectActor); if (FireEffectCast) { //Get the Particle Fire Component and deactivate it. FireEffectCast->GetParticleFireComponent()->Deactivate(); //Get the Fire Audio Component and deactivate it. FireEffectCast->GetFireAudioComponent()->Deactivate(); } } }
-
Compile your Code.
-
Select one of your BP_FireEffect instances in the Level Viewport, then in the Details panel navigate to the Tags variable, locate the FindActorTag field and open the dropdown menu next to it to delete the variable.
-
Navigate to the Toolbar and click Play (PIE).
End Result
When pressing the numeric 1 key, you should notice all Bluprint_Fire_ Effect particles with the ActorsToFindTag are extinguished, while the fire effect without the tag remains.
When pressing the numeric 1 key, you should notice all BP_FireEffect particles with the FindActorsTag are extinguished, while the fire effect without the tag remains.