2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【UE4】C++のコンストラクタからブループリントのアセットをロードする方法

Last updated at Posted at 2019-12-11

最初に

間違いや更に良い方法があった場合にはそっとTwitter( @ろっさむ )やコメント、修正リクエストなどでお知らせ頂けるととても有難いです。

作業環境

記事内で使用する作業環境は以下の通りとなります。

  • Unreal Engine:4.23.1
  • Visual Studio:Community 2017 or Community 2017 for Mac or Enterprise 2019

BPのパスを取得する

ロードしたいBPを右クリックでメニューを表示し、Copy Referenceを押下します。

image.png

コンストラクタでBPをロードする

ACPP_StartGameMode::ACPP_StartGameMode()
	:Super()
{
    // ロードを行いたいBPのパスを指定. これは
    // C++からBPのクラスを参照する際には末尾に_cを付け足す.
	FString CharacterPath = "/Game/ThirdPersonBP/Blueprints/ThirdPersonCharacter.ThirdPersonCharacter_c";
        
    // ConstructorHelpersはコンストラクタ内でオブジェクトやオブジェクトのクラスを検索する機能を持つ.
	// アセットが存在しないかエラーでロードできない場合、プロパティにはnullptrが入る。
	static ConstructorHelpers::FObjectFinder<UClass> CharacterBP(*CharacterPath);
	TSubclassOf<AActor> ThirdPersonCharacter = CharacterBP.Object;

	if (ThirdPersonCharacter != nullptr)
	{
		DefaultPawnClass = ThirdPersonCharacter;
	}
}

ここでパス名が間違っている(「'」が文頭、文末についていたり、「_c」が入っていない)場合には、ConstructorHelpersInternalFindOrLoadObjectでブレイクされてしまう可能性があります。注意しましょう。

この場合のロード方法だと、コンストラクタで上記のようなロードを行っているアセットを生成した際に、記述しているアセットも一気にロードされるため、メモリ使用量が膨れ上がることがあります。

こういった場合に、ランタイムでタイミングを見てロードするかどうか決める方法があるのでそちら次の記事で確認していきます。

次の記事:後程リンクを作成します

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?