Reading text file in Unreal Engine 4

Something that really bug me is inability of ue4 to handle or read simple text file. Yes, you still can do this. But, what if I want to do is just put simple file text and read that as a string. You can do that in C++ using

For the purpose of this tutorial, we will create text file reader as a actor component. We need a method to read file that will be called from blueprint.

Create class actor component

Name it “TextReaderComponent”.

Put this code in your header & implementation files.

UFUNCTION(BlueprintCallable) FString ReadFile(FString filename); 
FString UTextReaderComponent::ReadFile(FString filename)
{
	//Read file ini [project]/Content/Data/ 
        //you can change with other location
	FString directory = FPaths::Combine(FPaths::GameContentDir(), "Data");
	FString result;
	IPlatformFile& file = FPlatformFileManager::Get().GetPlatformFile();
	if (file.CreateDirectory(*directory)) {
		FString myFile = directory + "/" + filename;
		FFileHelper::LoadFileToString(result, *myFile);
	}
	return result;
}

Compile!

Compile in both Editor & Visual Studio.

Testing.

Create text file in Content/Data/file.txt. Fill it with whatever text you want. Add Component “Text Reader” in your actor. Drag component to Event Graph. Now you can read simple text file.