UE4でBluePrintでテキストファイルを読み込む方法(UE4.26)

以下のサイトを参考にさせていただきました。
https://horizonglow2.blogspot.com/2019/02/ue4.html

新規C++クラスを作成します。
作成するのは、成果物がBPで扱えるBlueprint Function Libraryになります。

フォルダやクラス名を適当に決めたら、VisualStudioが開きます。
まず.hファイルに定義を書きます。
ここではTextFileToStringというクラスを作成することにします。
書いたコードはこんな感じ。

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "TextFileToString.generated.h"

/**
 * 
 */
UCLASS()
class AMBITIONEARLYHOURSKY_API UTextFileToString : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

		UFUNCTION(BlueprintCallable, Category = "MyBPLibrary")
		static void TextFileLoadToString(FString Filename, FString& FileData, bool& Success);
	
};

CPPはこう

// Fill out your copyright notice in the Description page of Project Settings.


#include "TextFileToString.h"

// ScriptLoader.cpp

//#include "FileHelpers.h"
#include "Engine.h"


// ScriptLoader.cpp
void UTextFileToString::TextFileLoadToString(FString Filename, FString& FileData, bool& Success)
{
	if (GEngine)
	{
		FString FilePath = Filename;

		if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*FilePath))
		{
			Success = false;
			return;
		}

		const int64 FileSize = FPlatformFileManager::Get().GetPlatformFile().FileSize(*FilePath);
		FFileHelper::LoadFileToString(FileData, *FilePath);
		Success = true;

	}
	Success = false;
}

こんなノードの使い方します。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です