How can I load text from file with blueprint?

Hello, I want to load texts from .txt files using blueprints. Any way I can do it?

you can make blueprint nodes for saving and loading text files:
add code to your project inheriting from blueprintFunctionLibrary.

yourBlueprintFunctionLibrary.CPP

bool UyourBlueprintFunctionLibrary::FileSaveString(FString SaveTextB, FString FileNameB)
{
	return FFileHelper::SaveStringToFile(SaveTextB, *(FPaths::GameDir() + FileNameB));
}

bool UyourBlueprintFunctionLibrary::FileLoadString(FString FileNameA, FString& SaveTextA)
{
	return FFileHelper::LoadFileToString(SaveTextA, *(FPaths::GameDir() + FileNameA));
}

yourBlueprintFunctionLibrary.h

UFUNCTION(BlueprintCallable, Category = "save")
	static bool FileSaveString(FString SaveTextB, FString FileNameB);

UFUNCTION(BlueprintPure, Category = "save")
	static bool FileLoadString(FString FileNameA, FString& SaveTextA);

Thanks for the answer! I have succesfully added code to my project but new node is not showing when I try to add it.

if you added that code to your custom blueprintFunctionLibrary and compiled it, you should be able to access those nodes from any blueprint graph by searching for the “save” category, which should contain 2 functions named FileLoadString and FileSaveString.

I’m having trouble with this. Do you add this to code on the project or somewhare in the engine source code? And where exactly should this file be saved
Thanks for your time

Create a new c++ class “yourBlueprintFunctionLibrary” derived from UBlueprintFunctionLibrary and add the code to it

That’s cool :slight_smile:

How would the code looks like if I wanted to write multiple lines instead of everything on the same one?

And same for the the Read, i’d like to make a list out of the texts

Hello, i have no clue about c++, but i would like to load a string from a file into the engine with blueprints.
Following this instructions, i have added a new c++ class “yourBlueprintFunctionLibrary”, added the code at the end of the *.h and *.cpp file.
When compiling, i am getting an error “no class or namespace”,
in Microsoft Visual Studio Express 2013, i see

What am i doing wrong? I am working on 4.83, windows 8 64bit.

Thanks in advance

Hi there. I found the problem
Your file name is MyBlueprintFunctionLibrary and if you look at the code in the cpp file you wil see bool UyourBlueprintFunctionLibrary. Change that to the file name of your blueprint which in your case would be UMyBlueprintFunctionLibrary
Hope this helps post again if you still need furthur help on this

Yes! That was it! Thanks a lot :slight_smile:

I tried to replicate the code shown in showpixel’s question visible above, but I get the following errors (see screenshots). It doesn’t accept my class definition for T_LOAD_IT_API. It also doesn’t like UMyBlueprintFunctionLibrary even though the file names are MyBlueprintFunctionLibrary.h and MyBlueprintFunctionLibrary.cpp .

It throws the same exceptions with the ‘class constructor’ and ‘class destructor’ lines commented out; they were added there by UE4 automatically. Also putting them after the bools doesn’t help (and seems like an illogical order for those lines of code to be in if you ask me).

Hi Shrooblord,
i must say first that i have no clue about c++ programming.
But i did need to change the *.cpp code to make it work.
Please check the image (left side is your code , the right shows my code)

Best Regards!

So your including the t_load_it.h header file. But that’s, I assume, the name of your project right? The name of my project is FTL_Overdrive and as such I include the FTL_Overdrive.h header file. Was I not supposed to do that? Is the t_load_it.h an Epic-native Engine header file?

Yes. I generated it exactly as mentioned above.

That doesn’t work for me. I also don’t see what else is different about your code or mine. Note that the code on the left you display is not my code as you said, but rather the code mentioned further up in the page where there’s the typo “UyourBlueprintFunctionLibrary” instead of “UMyBlueprintFunctionLibrary”. I don’t have that typo in my files. See the screenshot I supplied above.

So the code still won’t compile (since there’s been no changes made to it…)

Excellent, but! I had to add public: after GENERATED_BODY() to be able to use the load function, like this:

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

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

/**
 * 
 */
UCLASS()
class DEMO2016_2_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()public:
	
		UFUNCTION(BlueprintCallable, Category = "save")
		static bool FileSaveString(FString SaveTextB, FString FileNameB);

	UFUNCTION(BlueprintPure, Category = "save")
		static bool FileLoadString(FString FileNameA, FString& SaveTextA);
	
	
};

Also bear in mind that the File Load String function will automatically search for files in the root directory, where your Content folder is on your drive.

I’m on 4.22 and the source compiles fine but the functions don’t show up. What am I doing wrong?

Try this on top in .cpp file

#include "Misc/FileHelper.h"
#include "Misc/Paths.h"

Is there a way to delete the file? I can’t find any function for that in FFileHelper