アクタを見つける

ブループリント スクリプト / 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」という名前にします。

    画像をクリックして拡大表示。

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

    画像をクリックして拡大表示。

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

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

    クリックしてフルサイズで表示

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

    クリックしてフルサイズで表示

FireEffect アクタを作成する

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

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

03_BPDragBPEffectFire.png

  1. [Add (追加)] ボタンをクリックし、新規 C++ クラス を作成し、[Choose a Parent Class (親クラスを選択)] メニューから [Actor (アクタ)] を選択して [Next (次へ)] をクリックします。

    クリックしてフルサイズで表示

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

    クリックしてフルサイズで表示

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

        public:
            //Fire サウンド エフェクト オーディオ コンポーネントを取得します。
            UAudioComponent* GetFireAudioComponent() const { return FireAudioComponent; }
    
            //Fire エフェクトのパーティクル システム コンポーネントを取得します。
            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. AFireEffect コンストラクタで、以下のコードを実装します。

        AFireEffect::AFireEffect()
        {
            // 各フレームでこのアクタが Tick() を呼び出すよう設定します。必要ない場合は、パフォーマンス向上のためにこれをオフにすることができます。
            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

  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:
        // このアクタのプロパティのデフォルト値を設定します。
        AFireEffect();

    protected:
        // ゲームの開始時またはスポーン時に呼び出します。
        virtual void BeginPlay() override;

    public:
        // フレームごとに呼び出します。
        virtual void Tick(float DeltaTime) override;

    public:
        //Fire サウンド エフェクト オーディオ コンポーネントを取得します。
        UAudioComponent* GetFireAudioComponent() const { return FireAudioComponent; }

        //Fire エフェクトのパーティクル システム コンポーネントを取得します。
        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"

    // デフォルト値を設定します。
    AFireEffect::AFireEffect()
    {
        // 各フレームでこのアクタが Tick() を呼び出すよう設定します。必要ない場合は、パフォーマンス向上のためにこれをオフにすることができます。
        PrimaryActorTick.bCanEverTick = true;
        // 各フレームでこのアクタが Tick() を呼び出すよう設定します。必要ない場合は、パフォーマンス向上のためにこれをオフにすることができます。

        PrimaryActorTick.bCanEverTick = true;
        ParticleFireComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("P_Fire"));
        FireAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Fire Audio"));
        ParticleFireComponent->SetupAttachment(RootComponent);
        FireAudioComponent->AttachToComponent(ParticleFireComponent, FAttachmentTransformRules::KeepRelativeTransform);
    }

    // ゲームの開始時またはスポーン時に呼び出します。
    void AFireEffect::BeginPlay()
    {
        Super::BeginPlay();
    }

    // フレームごとに呼び出します。
    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"

    Unreal Engine は Include-What-You-Use (IWYU) 依存関係モデルを使用します。つまり、エンジンのソース コードにはコンパイルに必要な依存関係のみが含まれることを意味します。追加のドキュメントについては、「IWYU」を参照してください。

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

        void AFindingActorsCharacter::OnFindActorPressed()
        {
            TArray<AActor*> ActorsToFind;
            if(UWorld* World = GetWorld())
            {
            UGameplayStatics::GetAllActorsOfClass(GetWorld(), AFireEffect::StaticClass(), ActorsToFind);
            }
            for (AActor* FireEffectActor: ActorsToFind)
            {
                //これは FireEffect クラスのアクタ タイプか?
                AFireEffect* FireEffectCast = Cast<AFireEffect>(FireEffectActor);
                if (FireEffectCast)
                {
                    //パーティクル ファイア コンポーネントを取得し、無効化します。
                    FireEffectCast->GetParticleFireComponent()->Deactivate();
    
                    //ファイア オーディオ コンポーネントを取得し、無効化します。
                    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()

        /** キャラクターの後ろにカメラを配置するカメラ ブーム*/
        UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
        class USpringArmComponent* CameraBoom;

        /** カメラをフォローします。*/
        UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
        class UCameraComponent* FollowCamera;

    public:

        AFindingActorsCharacter();

        /** 基本回転速度 (度/秒)。最終的なターン率が他のスケーリングの影響を受ける場合があります。*/
        UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Input)
        float TurnRateGamepad;

    protected:

        /** 前方/後方入力のために呼び出されます。*/
        void MoveForward(float Value);

        /** 左右の入力のために呼び出されます。*/
        void MoveRight(float Value);

        /**
         * 任意の率でターンするよう、入力を介して呼び出されます。
         * @param Rate  これは正規化された率で、「1.0」は希望のターン率を 100% 実現していることを意味します。
         */
        void TurnAtRate(float Rate);

        /**
         * 任意の率でルックアップ/ルックダウンをターンするよう、入力を介して呼び出されます。
         * @param Rate  これは正規化された率で、「1.0」は希望のターン率を 100% 実現していることを意味します。
         */
        void LookUpAtRate(float Rate);

        /** タッチ入力が開始されたときのハンドラ。*/
        void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location);

        /** タッチ入力が停止したときのハンドラ。*/
        void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location);

    protected:

        // APawn インターフェース
        virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
        // APawn インターフェースの終了

    public:

        /** CameraBoom サブオブジェクトを返します。**/
        FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }

        /** FollowCamera サブオブジェクトを返します。**/
        FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }

    protected:

        /** プレイヤーの入力コンポーネントから入力キーが押されたときに呼び出されます。*/
        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()
    {
        // コリジョン カプセルのサイズを設定します。
        GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);

        // 入力向けのターン率を設定します。
        TurnRateGamepad = 50.f;

        // コントローラーが回転するときは回転させないでください。影響を受けるのはカメラのみにします。
        bUseControllerRotationPitch = false;
        bUseControllerRotationYaw = false;
        bUseControllerRotationRoll = false;

        // キャラクターの動きを設定します。
        GetCharacterMovement()->bOrientRotationToMovement = true; // 入力した方向にキャラクターが移動します...
        GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...この回転速度で実行します。

        // 注:これらの変数のイテレーションをさらに高速化する場合などは、再コンパイルして調整するのではなく、キャラクターの
        // ブループリントで微調整できます
        GetCharacterMovement()->JumpZVelocity = 700.f;
        GetCharacterMovement()->AirControl = 0.35f;
        GetCharacterMovement()->MaxWalkSpeed = 500.f;
        GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
        GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;

        // カメラ ブームを作成します (コリジョンがある場合はプレイヤーに向かって引きます)。
        CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
        CameraBoom->SetupAttachment(RootComponent);
        CameraBoom->TargetArmLength = 400.0f; // カメラがキャラクターの後ろをこの距離で追跡します。
        CameraBoom->bUsePawnControlRotation = true; // コントローラーを基準にアームを回転させます。

        // 追跡するカメラを作成します。
        FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
        FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // ブームの最後にカメラをアタッチして、コントローラーの方向と一致するようブーム自身に調整させます。
        FollowCamera->bUsePawnControlRotation = false; // カメラは腕の動きに合わせて回転しません。

        // 注:メッシュ コンポーネント上のスケルタルメッシュと Anim ブループリントの参照 (キャラクターから継承) は、
        // ThirdPersonCharacter という名前の、派生したブループリント アセットで設定されます (C++ での直接のコンテンツ参照を回避するため)
    }

    //////////////////////////////////////////////////////////////////////////
    // 入力

    void AFindingActorsCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
    {
        // ゲームプレイ キー バインディングを設定します。
        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);

        // さまざまなデバイスをそれぞれ処理するための回転バインディングには 2 つのバージョンがあります。
        // 「turn」では、マウスなどの絶対デルタを提供するデバイスを処理します。
        // 「turnrate」は、アナログのジョイスティックなど、ユーザーが「変更率」として処理することに決めたデバイス向けです。
        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);

        // タッチ デバイスを処理します。
        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)
    {
        // 率に関する情報からこのフレームのデルタを計算します。
        AddControllerYawInput(Rate * TurnRateGamepad * GetWorld()->GetDeltaSeconds());
    }

    void AFindingActorsCharacter::LookUpAtRate(float Rate)
    {
        // 率に関する情報からこのフレームのデルタを計算します。
        AddControllerPitchInput(Rate * TurnRateGamepad * GetWorld()->GetDeltaSeconds());
    }

    void AFindingActorsCharacter::MoveForward(float Value)
    {
        if ((Controller != nullptr) && (Value != 0.0f))
        {
            // どちらが前方かを調べます。
            const FRotator Rotation = Controller->GetControlRotation();
            const FRotator YawRotation(0, Rotation.Yaw, 0);

            // get forward ベクター
            const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
            AddMovementInput(Direction, Value);
        }
    }

    void AFindingActorsCharacter::MoveRight(float Value)
    {
        if ( (Controller != nullptr) && (Value != 0.0f) )
        {
            // どちらが右かを調べます。
            const FRotator Rotation = Controller->GetControlRotation();
            const FRotator YawRotation(0, Rotation.Yaw, 0);

            // get right ベクター
            const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
            // その方向に動きを追加します。
            AddMovementInput(Direction, Value);
        }
    }

    void AFindingActorsCharacter::OnFindActorPressed()
    {
        TArray<AActor*> ActorsToFind;
        if (UWorld* World = GetWorld())
        {
            UGameplayStatics::GetAllActorsOfClass(GetWorld(), AFireEffect::StaticClass(), ActorsToFind);
        }
        for (AActor* FireEffectActor : ActorsToFind)
        {
            //これは FireEffect クラスのアクタ タイプか?
            AFireEffect* FireEffectCast = Cast<AFireEffect>(FireEffectActor);
            if (FireEffectCast)
            {
                //パーティクル ファイア コンポーネントを取得し、無効化します。
                FireEffectCast->GetParticleFireComponent()->Deactivate();

                //ファイア オーディオ コンポーネントを取得し、無効化します。
                FireEffectCast->GetFireAudioComponent()->Deactivate();
            }
        }
    }

最終結果

  1. ツールバー に移動し、 [Play (プレイ) (PIE)] をクリックします。

16_PlayButton.png

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

  1. コンテンツ ブラウザ に移動し、いくつかの BP_FireEffect インスタンスを レベル内 にドラッグします。

    クリックしてフルサイズで表示

  2. ツールバー に移動し、[Play (プレイ)] (PIE) をクリックします。

    11_PlayButton.png

  3. 数字キーの 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 の GetAllActorsofClass 関数を使用し、特定のクラスのアクタの配列を取得しました。しかし、さまざまな条件に基づいて配列の結果をフィルタし、アクタの タグ を使用することで、その配列から特定の複数のアクタや単一のアクタを取得することもできます。

  1. まず、コンテンツ ブラウザ > 「C++ Classes」 フォルダに移動し、FireEffect Actor アクタ をクリックして「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 をダブルクリックして「FindingActorsCharacter.cpp」ファイルを開きます。

    14_OpenFindingActors.png

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

        void AFindingActorsCharacter::OnFindActorPressed()
        {
    
            TArray<AActor*> ActorsToFind;
    
            //「FindActorTag」を持つ FireEffect クラスのすべてのアクタを取得します
            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. このコードを コンパイル します。

  8. レベル ビューポート のいずれかの BP_FireEffect インスタンスを選択し、[Details] パネルから 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 のドキュメントを改善するために協力をお願いします!どのような改善を望んでいるかご意見をお聞かせください。
調査に参加する
キャンセル