Finding Actors

A tutorial on how to find Actors in your scene by using Blueprint Scripting / C++.

Choose your operating system:

Windows

macOS

Linux

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.

image alt text

In the image above, the World Outliner (right) represents all the Actors that are placed inside your level. Clicking on one of the Actors in the World Outliner highlights it inside the level viewport as well. You can sort the World Outliner by Actor Name or by Actor Type by clicking on the Actor or Type headers (you can change the secondary category from Type to other filters by clicking the down arrow in the Type Box).

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.

Choose your implementation method:

Blueprints

C++

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.

  1. Begin by creating a New > Games > ThirdPerson > Blueprint Project with Starter Content, named FindingActors.

    image alt text

  2. 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.

    image alt text

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.

  1. Begin by creating a New > Games > ThirdPerson > C++ Project with Starter Content named FindingActors.

    image alt text

  2. 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.

    image alt text

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.

  1. Navigate to Content > StarterContent > Blueprints, then drag an instance of Blueprint_Effect_Fire into the World.

    image alt text

  1. Click the Add/Import button to create a New C++ Class then from the Choose a Parent Class menu, select Actor to create a new Actor class named FireEffect.

    image alt text

  2. 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* ParticleFire;
    
    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
    UAudioComponent* FireAudio;
  3. Navigate to the FireEffect.cpp file, then include the following class libraries:

    #include "Particles/ParticleSystemComponent.h"
    #include "Components/AudioComponent.h"
  4. In the FireEffect 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;
    
        ParticleFire = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("P_Fire"));
    
        FireAudio = CreateDefaultSubobject<UAudioComponent>(TEXT("Fire Audio"));
    
        ParticleFire->SetupAttachment(RootComponent);
    
        FireAudio->AttachToComponent(ParticleFire,FAttachmentTransformRules::KeepRelativeTransform);
    
    }
  5. Compile your code.

  6. Navigate to Content Browser > C++ Classes > FindingActors and right-click the FireEffect Actor to Create a Blueprint class based on FireEffect named BP_FireEffect**.

    image alt text

  7. 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.

    image alt text

  8. 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.

    image alt text

  9. Click Compile and Save.

    image alt text

Finished Code

    FireEffectActor.h

    #pragma once

    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "FireEffect.generated.h"

    UCLASS()

    class FINDINGACTORS_API AFireEffect : public AActor

    {

        GENERATED_BODY()
    public: 

        // Sets default values for this actor's properties

        AFireEffect();

        UAudioComponent* GetFireAudioComponent() const { return FireAudioComponent; }

        UParticleSystemComponent* GetParticleFireComponent() const { return ParticleFireComponent; }

    protected:

        // Called when the game starts or when spawned

        virtual void BeginPlay() override;

        UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)

        UParticleSystemComponent* ParticleFireComponent;

        UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)

        UAudioComponent* FireAudioComponent;

    public: 

        // Called every frame

        virtual void Tick(float DeltaTime) override;

    };

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;

        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

  1. Navigate to the Content > ThirdPersonBp > Blueprints folder and double click the ThirdPersonCharacter to open its Class Defaults.

    image alt text

  2. Right-click on the Event Graph, then from the Actions menu search for the FindActorPressed action key event.

    image alt text

  3. 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.

    image alt text

  4. Inside the Get All Actors of Class node, click the Actor Class, then from the drop-down select the Blueprint_Effect_Fire class.

    image alt text

  5. Drag off from the Out Actors pin and in the Actions menu, search for ForEachLoop.

    image alt text

  6. 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.

    image alt text

  7. From the ForEachLoop node, drag off the Array Element pin, then in the Actions menu, search for Get P_Fire.

    image alt text

  8. Drag off from the Array Element pin, then in the Actions menu, search for Get Fire Audio.

    image alt text

  9. Drag off from the P Fire pin, then in the Actions menu search for the Deactivate node.

    image alt text

  10. Drag off from the Fire Audio pin and connect it to the Target pin from the Deactivate node.

    image alt text

  11. Click Compile and Save.

    image alt text

  1. From the Content Browser, navigate to C++ Classes > FindingActors > and double-click FindingActorsCharacter to open the FindingActorsCharacter.h file.

    image alt text

  2. In the FindingActorsCharacter class defaults, declare the following:

    protected:
    void OnFindActorPressed();
  3. 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.

  4. 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();
            }   
        }
    }
  5. Navigate to the SetupPlayerInputComponent method and declare the following code:

    void AFindingActorsCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
    
    {
    
        PlayerInputComponent->BindAction("FindActorPressed", IE_Pressed, this, &AFindingActorsCharacter::OnFindActorPressed);
    
    }
  6. Compile your code.

Finished Blueprint

image alt text

Finished Code

FindingCharacters.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=Camera)

        float BaseTurnRate;

        /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */

        UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)

        float BaseLookUpRate;

    protected:

        /** Resets HMD orientation in VR. */

        void OnResetVR();

        /** 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);

        /** Called when Input key is pressed from the Player input component */

        void OnFindActorPressed();

    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; }

    };

End Result

  1. Navigate to the Toolbar and click Play (PIE).

    image alt text

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.

image alt text

  1. Navigate to Content Browser, and drag a few instances of your BP_FireEffect into the Level.

    image alt text

  2. Navigate to the Toolbar and click Play (PIE).

    image alt text

  3. 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.

    image alt text

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. As an example,

  1. Navigate to the Content > StarterContent > Blueprints and select Blueprint_Effect_Fire.

    image alt text

  2. In the Details panel, navigate to the Tags section and click Add(+) to add a tag to the Actor.

    image alt text

  3. In the 0 element field, enter "FindActorTag" into the text string.

    image alt text

  4. Inside the MyCharacter Blueprint, drag off from the Array Element pin of the ForEachLoop and in the Actions menu search for Get Tags.

    image alt text

  5. Drag off from the Tags pin, then in the Actions menu search for the Get node.

    image alt text

    Here we are "Getting" the Tag at Index 0 which we set as FindActorTag above.

  6. Right-click in the graph and in the Actions menu, search for the Branch node.

    image alt text

  7. Drag off from the Condition pin of the Branch node and add an Actor Has Tag node.

    image alt text

  8. Connect the out pin from the Get node to the Tag pin on the Actor Has Tag node.

    image alt text

  9. Connect the Array Element pin of the ForEachLoop node to the Target pin on the Actor Has Tag node.

    image alt text

  10. Drag off from the Array Element of the ForEachLoop pin twice to get P_Fire and Fire Audio nodes and connect them to a Deactivate node.

    image alt text

  11. Click Compile and Save.

    image alt text

  12. Select one of your Blueprint_FireEffect's from the Level viewport, 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.

    image alt text

    We are intentionally leaving one of the fire effects without a tag in order to demonstrate the functionality.

  13. Navigate to the Toolbar and Press Play (PIE).

    image alt text

You used the Get All Actors of Class 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. As an example,

  1. Begin by navigating to your Content Browser > C++ Classes folder, and double click your FireEffect Actor to open its FireEffect.cpp file.

    image alt text

  2. 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.

  3. Compile your code.

  4. Open your BP_FireEffect, then in the Details panel, navigate to the Actor category and you will notice your Actor Tag has been created.

    image alt text

  5. Navigate to your Content Browser > C++ Classes Folder, and double-click your FindingActorsCharacter to open its .cpp file.

    image alt text

  6. 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)
    
            {
    
             FireEffectCast->GetParticleFireComponent()->Deactivate();           FireEffectCast->GetFireAudioComponent()->Deactivate();
    
            }   
    
        }
    
    }
  7. Compile your Code.

  8. 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.

    image alt text

  9. Navigate to the Toolbar and click Play (PIE).

    image alt text

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.

image alt text

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.

image alt text