アクタを見つける

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

Choose your operating system:

Windows

macOS

Linux

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

image alt text

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

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

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

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

Blueprints

C++

プロジェクトの設定

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

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

image alt text

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

image alt text

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

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

image alt text

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

image alt text

FireEffect アクタを作成する

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

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

image alt text

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

image alt text

  1. 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* ParticleFire;
    
    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
    UAudioComponent* FireAudio;
  2. FireEffect.cpp 」ファイルに移動して、次のクラス ライブラリをインクルードします。

    #include "Particles/ParticleSystemComponent.h"
    #include "Components/AudioComponent.h"
  3. 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;
    
        ParticleFire = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("P_Fire"));
    
        FireAudio = CreateDefaultSubobject<UAudioComponent>(TEXT("Fire Audio"));
    
        ParticleFire->SetupAttachment(RootComponent);
    
        FireAudio->AttachToComponent(ParticleFire,FAttachmentTransformRules::KeepRelativeTransform);
    
    }
  4. このコードを コンパイル します。

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

image alt text

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

image alt text

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

image alt text

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

image alt text

完成コード

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

    }

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

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

  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 を参照してください。

  1. 次のコードを宣言して、 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();
            }   
        }
    }
  2. SetupPlayerInputComponent メソッドに移動して以下のコードを宣言します。

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

完成したブループリント

image alt text

完成コード

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

    };

最終結果

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

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

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

image alt text

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

  1. グラフ内を 右クリック し、 [Actions] メニューで Branch ノードを検索します。

image alt text

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

image alt text

  1. Get ノードの out ピンを Actor Has Tag ノードの Tag ピンに接続します。

image alt text

  1. ForEachLoop ノードの Array Element ピンを Actor Has Tag ノードの Target ピンに接続します。

image alt text

  1. ForEachLoop Array Element ピンを 2 回引き出し、 P_Fire ノードと Fire Audio ノードに接続し、それらを Deactivate ノードに接続します。

image alt text

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

image alt text

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

image alt text

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

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

image alt text

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

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

image alt text

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

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

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

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

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

image alt text

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

image alt text

  1. 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)
    
            {
    
             FireEffectCast->GetParticleFireComponent()->Deactivate();           FireEffectCast->GetFireAudioComponent()->Deactivate();
    
            }   
    
        }
    
    }
  2. このコードを コンパイル します。

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

image alt text

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

image alt text

最終結果

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

image alt text

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

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