FFileHelper::LoadFileToString does not work on packaged project

Hi,

I want my game to be able to read a text file in runtime (during startup). Let’s say it’s located in D:/Document/test.txt. Therefore, I am using FFileHelper::LoadFileToString. I do not currently understand the limitation of IO that UE4 and FFileHelper and other file parsing libraries in UE4 have, but when I start the game in editor, and give an absolute path to a file, LoadFileToString works perfectly. It sees the file and its content. However, when I package the game and run it, it does not. Because it’s lacking documentation and error variables and others, I wonder what I did wrong? Can UE4 game read arbitrary files in any arbitrary locations? Or is there a limitation to this? I tried to package it into development and shipping versions, both do not work.

PlatformFile.FileExists returns false too, I wonder why.

My goal is to actually create a custom INI file somewhere in the game directory (that contains the exe), that the game can read during startup. I tried using GConfig->GetInt and other methods but they all do not work when the game is packaged. They all work fine when I run the game in the editor.

Have you tried relative path?

FString FileName(TEXT("MyConfig.ini"));
FString Result;
FFileHelper::LoadFileToString(Result, *(FPaths::ProjectConfigDir() + FileName));
1 Like

I think the ProjectConfigDir is empty in packaged build? At least in mine.

FFileHelper::SaveStringToFile(TEXT(“testing”), TEXT(“save.ini”));
FFileHelper::LoadFileToString(Result, TEXT(“load.ini”));
SaveStringToFile works, and it creates a save.ini file next to the exe in Binaries/Win64, but LoadFileToString does not.

Update: cancel that, it works.

So what I did was I created a code to load configuration from a file in C++ and exposed it as blueprint function. The function is then called in a custom game instance that I created. I called it in Event Init. It does not work when I call the function in Event Init. It does however work when I call the function in Event Begin Play in level blueprints. Anyone mind to explain why?

I also had the same error.
It’s an old topic, but I’m writed a solution for users who come to see it.
In the case of my problem, read the contents of the file and confirmed that there was an error in processing the data.
I processed the data through pointer reference, but I think it fails to read the file because the character encoding is not processed.
After modifying the part that processes the data, it reads properly.
I hope this answer will be helpful to you.