アクタを見つける

ブループリント スクリプト / C++ を使ってシーンでアクタを見つける方法のチュートリアル。

プロジェクトの規模が大きくなり始めると、シーンの中で個々の アクタ を見つけるのが難しくなる場合があります。通常シーンの中には、レベルセットのピースやジオメトリから、NPC、敵、インタラクト可能なオブジェクト、ピックアップに至るまで、シーンに数多くのアクタがあります。 Unreal Editor で作業しているときは、ワールド アウトライナ を使用してシーン内の俳優を見つけるのに役立てることができます。

image alt text

上の右側のアウトライナの画像には、現在のレベルに配置されているすべてのアクタが表示されます。アウトライナの中のアクタの 1 つをクリックすると、レベル ビューポートないのアクタも強調表示されます。アクタ または タイプ ヘッダーをクリックして、アクタ名またはアクタ タイプでワールドアウトライナーを並べ替えることができます (タイプ ボックスの下向き矢印をクリックすると、セカンダ リカテゴリをタイプから他のフィルタに変更できます)。

アウトライナでアクタを選択し、レベル ビューポート内を移動しているときに、アクタをダブルクリックするか、F キーを押すと、フォーカス するように選択したアクタの位置にカメラが移動しします。

詳細については、「ワールド アウトライナ」を参照してください。

実装方法を選んでください。

Blueprints

C++

プロジェクトの設定

このチュートリアルでは、 Get All Actors of Class ノードを使用し、レベル内のアクタを見つけます。このノードを呼び出すと、指定したクラスのレベル内のすべてのアクタが取得され、 配列 に格納されます。続いて、その配列からフィルタの条件に基づいて単一または複数のアクタを取得できます。さらに、それらアクタのプロパティにアクセスしたり、実現したい機能に基づいていくつかの方法によってプロパティを変更したりすることができます。

  1. まず、 [New (新規)] > [Games (ゲーム)] > [ThirdPerson (サードパーソン)] > [Blueprint (ブループリント)] プロジェクトを [With Starter Content (スターターコンテンツ有り)] で作成し、「FindingActors」という名前にします。

    Click for full view.

  2. [Edit (編集)] > [Project Settings (プロジェクト設定)] > [Engine (エンジン)] > [Input (入力)] > [Bindings (バインディング)] > [Action Mappings (アクション マッピング)] の順に移動し、 [Add (追加) (+)] をクリックして「FindActorPressed」という名前の新しいアクション マッピングを作成し、その キー値1 に設定します。

    Click for full view.

このチュートリアルでは、 Gameplay Statics Class Library (Get All Actors of Class ノード) を使用し、レベル 内の アクタ を見つけます。この関数 (ノード) を呼び出すと、指定した クラス のレベル内のすべてのアクタが取得され、 配列 に格納されます。続いて、その配列から任意のフィルタの条件に基づいてすべてまたは単一のアクタを取得できます。さらに、それらアクタのプロパティにアクセスしたり、実現したい機能に基づいていくつかの方法によってプロパティを変更したりすることができます。

  1. まず、 [New (新規)] > [Games (ゲーム)] > [ThirdPerson (サードパーソン)] > [C++] プロジェクトを [With Starter Content (スターターコンテンツ有り)] で作成し、「FindingActors」という名前にします。

    Click for full view.

  2. [Edit (編集)] > [Project Settings (プロジェクト設定)] > [Engine (エンジン)] > [Input (入力)] > [Bindings (バインディング)] > [Action Mappings (アクション マッピング)] の順に移動し、 [Add (追加) (+)] をクリックして「FindActorPressed」という名前の新しいアクション マッピングを作成し、その キー値1 に設定します。

    Click for full view.

FireEffect アクタを作成する

Starter Content」フォルダには、完成した FireEffect アクタが用意されており、このアクタにはフレーム エフェクトを表示する Particle コンポーネント と、サウンド エフェクト用の Audio コンポーネント が含まれています。このアクタは、Get All Actors of Class ノードから検索するベース アクタ クラスとして使用します。

  1. [Content (コンテンツ)] > [StarterContent (スターター コンテンツ)] > [Blueprints (ブループリント)] に移動し、 Blueprint_Effect_Fire のインスタンスをワールドにドラッグします。

![](03_BPDragBPEffectFire.png)

  1. [Add/Import (追加/インポート)] ボタンをクリックし、新規 C++ クラス を作成し、[Choose a Parent Class (親クラスを選択)] メニューから [Actor (アクタ)] を選択して「FireEffect」という名前の新しい Actor クラスを作成します。

    Click for full view.

  2. 作成したアクタクラスに「FireEffect」と名前をつけて Create Class をクリックします。

    Click for full view.

  3. FireEffect.h で、以下を宣言します。

    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;
  4. FireEffect.cpp」ファイルに移動して、次のクラス ライブラリをインクルードします。

    #include "Particles/ParticleSystemComponent.h"
    #include "Components/AudioComponent.h"
  5. FireEffect コンストラクタで、以下のコードを実装します。

    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);
    }
  6. このコードを コンパイル します。

  7. コンテンツ ブラウザ > [C++ Classes (C++ クラス)] > FindingActors に移動し、 FireEffect アクタをダブルクリックして FireEffect をベースにしたブループリント クラスを作成し、「BP_FireEffect」という名前にします。

    ![](05_CreateBPClassBased.png)(w:600)

  8. BP_FireEffect[Class Defaults (クラスのデフォルト)] で、 [Components (コンポーネント)] パネルの [Particle Fire (パーティクル ファイア)] をクリックし、 [Details (詳細)] パネルに移動します。続いて、 [Particles (パーティクル)] カテゴリで [Template (テンプレート)] ドロップダウン メニューを開き、 P_Fire を選択します。

    ![](06_AddTemplateToBPFireEffect.png)

  9. [Components] パネルの [Fire Audio (ファイア オーディオ)] をクリックし、 [Details] パネルに移動します。[Sound (サウンド)] カテゴリから Sound 変数のドロップダウン メニューを開き、 Fire01_Cue を選択します。

    ![](07_AddSoundToBPFireEffect.png)

  10. [Compile (コンパイル)][Save (保存)] を順にクリックします。

    ![](08_CompileSaveButton.png)

完成コード

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

サードパーソン キャラクター:Finding Actor Pressed Event を設定する

  1. Content」 > 「ThirdPersonBp」 > 「Blueprints」フォルダの順に移動し、 ThirdPersonCharacter をダブルクリックしてその [Class Defaults (クラスのデフォルト)] を開きます。

    ![](04_OpenBPThirdPersonCharacter.png)

  2. イベント グラフ を右クリックし、 [Actions (アクション)] メニューから FindActorPressed アクションのキー イベントを検索します。

    Copy Node Graph

    ![](05_BPScript01.png)

  3. FindActorPressed ノードのキー イベントから Pressed ピンを引き出し、 [Actions] メニュー から Get All Actors Of Class ノードを検索します。

    Copy Node Graph

    ![](06_BPScript02.png)

  4. Get All Actors of Class ノード内で [Actor Class (アクタ クラス)] をクリックし、ドロップダウン メニューから Blueprint_Effect_Fire クラスを選択します。

    ![](07_BPScript03.png)

  5. Out Actors ピンを引き出し、 [Actions] メニューで ForEachLoop を検索します。

    Copy Node Graph

    ![](08_BPScript04.png)

  6. Get All Actors Of Class ノードの Execution ピンを引き出し、 For Each Loop ノードの Exec ピンに接続します。

    Copy Node Graph

    ![](09_BPScript05.png)

  7. ForEachLoop ノードから Array Element ピンを引き出し、 [Actions] メニューで Get P_Fire を検索します。

    Copy Node Graph

    ![](10_BPScript06.png)

  8. Array Element ピンを引き出し、 [Actions] メニューで Get Fire Audio を検索します。

    Copy Node Graph

    ![](11_BPScript07.png)

  9. P Fire ピンを引き出し、 [Actions] メニューで Deactivate ノードを検索します。

    Copy Node Graph

    ![](12_BPScript08.png)

  10. Fire Audio ピンを引き出し、 Deactivate ノードの Target ピンに接続します。

    Copy Node Graph

    ![](13_BPScript09.png)

  11. [Compile (コンパイル)][Save (保存)] を順にクリックします。

    ![](14_CompileSaveButton.png)

  1. コンテンツ ブラウザ から、「C++ Classes」 > 「FindingActors」に移動し、 FindingActorsCharacter をダブルクリックして「FindingActorsCharacter.h」ファイルを開きます。

    ![](09_OpenFindingActors.png)

  2. FindingActorsCharacter クラスのデフォルト内で、以下を宣言します。

    protected:
        void OnFindActorPressed();
  3. FindingActorsCharacter.cpp に移動し、以下のクラス ライブラリをインクルードします。

    #include "Kismet/GameplayStatics.h"
    #include "FireEffect.h"
    #include "Particles/ParticleSystemComponent.h"
    #include "Components/AudioComponent.h"

    IWYU) 依存関係モデルを使用します。つまり、エンジンのソース コードにはコンパイルに必要な依存関係のみが含まれることを意味します。詳細は「IWYU」を参照してください。

  4. 次のコードを宣言して、 OnFindActorPressed のロジックを実装します。

    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. SetupPlayerInputComponent メソッドに移動して以下のコードを宣言します。

    void AFindingActorsCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
    {
        PlayerInputComponent->BindAction("FindActorPressed", IE_Pressed, this, &AFindingActorsCharacter::OnFindActorPressed);
    }
  6. コードを コンパイル します。

完成したブループリント

Copy Node Graph

![](15_BPScriptFinal1.png)

完成コード

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

最終結果

  1. ツールバー に移動し、

    [Play (プレイ)] (PIE)
    をクリックします。

![](16_PlayButton.png)

数字キーの 1 を押すと、Fire エフェクトのパーティクルが消去され、オーディオ コンポーネントから流れるサウンド エフェクトが再生されなくなります。

タグで特定のアクタを取得する

Get All Actors of Class ノードを使用し、特定のクラスのアクタの配列を取得しました。しかし、さまざまな条件に基づいて配列の結果をフィルタし、アクタの タグ を使用することで、その配列から特定の複数のアクタや単一のアクタを取得することもできます。次に例を示します。

  1. [Content (コンテンツ)] > [StarterContent (スターター コンテンツ)] > [Blueprints (ブループリント)] に移動して Blueprint_Effect_Fire を選択します。

    ![](18_SelectBPEffectFire.png)

  2. [Details (詳細)] パネルで、 [Tags (タグ)] セクションに移動し、 [Add(+)] をクリックしてアクタにタグを追加します。

    ![](19_BPEffectFireDetails.png)

  3. 0 の要素フィールドで、テキスト文字列として「FindActorTag」を入力します。

    ![](20_BPEffectFireTag.png)

  4. MyCharacter ブループリント内で ForEachLoopArray Element ピンを引き出し、 [Actions] メニューで Get Tags を検索します。

    Copy Node Graph

    ![](21_BPScript10.png)

  5. Tags ピンを引き出し、 [Actions] メニューで Get ノードを検索します。

    Copy Node Graph

    ![](22_BPScript11.png)

    上記の FindActorTag として設定したインデックス 0 のタグをここで取得します。

  6. Your Blueprint Script should look as following:

    Copy Node Graph

    ![](23_BPScript12.png)

  7. For Each Loop ノードの 実行 ピンを引き出し、Actions メニューから Branch ノードを探します。

    Copy Node Graph

    ![](24_BPScript13.png)

  8. Branch ノードの Condition ピンを引き出し、 Actor Has Tag ノードを追加します。

    Copy Node Graph

    ![](25_BPScript14.png)

  9. Connect the out pin from the Get ノードの out ピンを Actor Has Tag ノードの Tag ピンに接続します。

    ![](26_BPScript15.png)

  10. For Each Loop ノードの Array Element ピンを Actor Has Tag ノードの Target ピンに接続します。ブループリントスクリプトは以下のようになります。

    ![](27_BPScript16.png)

  11. Branch ノードの True ピンを Deactivate ノードの 実行ピン に接続します。

    ![](28_BPScript17.png)

  12. ブループリントスクリプトは以下のようになります。

    Copy Node Graph

    ![](29_BPScriptFinal2.png)

  13. [Compile (コンパイル)][Save (保存)] を順にクリックします。

    ![](30_CompileSaveButton.png)

  14. レベル ビューポート からいずれかの Blueprint_FireEffect を選択し、 [Details] パネル から Tags 変数に移動します。[FindActorTag] フィールドを見つけて ドロップダウン メニュー を開き、 [Delete (削除)] を選択してタグ変数を削除します。

    ![](31_BPEffectFireDelTag.png)

    ここではその機能を示すため、Fire エフェクトの 1 つは意図的にタグ付けしないままにしています。

  15. ツールバー に移動し、 [Play (PIE)] を押します。

    ![](32_PlayButton.png)

Gameplay Statics Library の Get All Actors of Class 関数を使用し、特定のクラスのアクタの配列を取得しました。しかし、さまざまな条件に基づいて配列の結果をフィルタし、アクタの タグ を使用することで、その配列から特定の複数のアクタや単一のアクタを取得することもできます。次に例を示します。

  1. まず、 コンテンツ ブラウザ > 「C++ Classes」フォルダに移動し、FireEffect アクタ をクリックして「FireEffect.cpp」ファイルをダブルクリックします。

    ![](12_OpenFireEffect.png)

  2. FireEffect コンストラクタ 内に、次のコードを追加します。

    AFireEffect::AFireEffect()
    {
        Tags.Add(FName("FindActorTag"));
    }

    Tags は、アクタのクラスのデフォルトの一部となる配列で、グループ化およびカテゴリ化に使用できます。

  3. このコードを コンパイル します。

  4. BP_FireEffect を開き、 [Details] パネルで [Actor (アクタ)] カテゴリに移動すると、アクタのタグが作成されています。

    ![](13_AddedActorTag.png)

  5. まず、 コンテンツ ブラウザ > 「C++ Classes」フォルダに移動し、 FindingActorsCharacter をダブルクリックして「.cpp」ファイルを開きます。

    ![](14_OpenFindingActors.png)

  6. OnFindActorPressed メソッドで、以下を実装します。

    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();
            }   
        }
    }
  7. このコードを コンパイル します。

  8. レベル ビューポート のいずれかの BP_FireEffect インスタンスを選択し、 [D****etails] パネルから Tags 変数に移動します。[FindActorTag] フィールドを見つけて横にあるドロップダウン メニューを開き、変数を削除します。

    ![](15_BPEffectFireDelTag.png)

  9. ツールバー に移動し、 [Play (プレイ)] (PIE) を押します。

    ![](16_PlayButton2.png)

最終結果

数字キーの 1 を押すと、ActorsToFindTag を持つすべての Bluprint_Fire_ Effect パーティクルが消去される一方で、タグがない Fire エフェクトはそのままになります。

数字キーの 1 を押すと、FindActorsTag を持つすべての BP_FireEffect particles パーティクルが消去される一方で、タグがない Fire エフェクトはそのままになります。

Unreal Engine のドキュメントを改善するために協力をお願いします!どのような改善を望んでいるかご意見をお聞かせください。
調査に参加する
キャンセル