Saved games and persistent data storage

I am trying to find analogue for Application.persistentDataPath from Unity3D.

I want to store user profile’s data and saved games in Users directory for Windows platform and in the data directory for Android (IOS).
What is the correct way to do this with UE4? Is it right to keep the profile’s data in Saved directory?

>Using the External Storage

I found some low level code in AndroidJNI.cpp:

jclass EnvClass = Env->FindClass("android/os/Environment");
jmethodID getExternalStorageDir = Env->GetStaticMethodID(EnvClass, "getExternalStorageDirectory", "()Ljava/io/File;");
jobject externalStoragePath = Env->CallStaticObjectMethod(EnvClass, getExternalStorageDir, nullptr);
jmethodID getFilePath = Env->GetMethodID(Env->FindClass("java/io/File"), "getPath", "()Ljava/lang/String;");
jstring pathString = (jstring)Env->CallObjectMethod(externalStoragePath, getFilePath, nullptr);
const char *nativePathString = Env->GetStringUTFChars(pathString, 0);
// Copy that somewhere safe 
GFilePathBase = FString(nativePathString);

But this method is too much lowlevel.

Is it possible to use this function directly? How to do that properly?



Are you possibly looking for: FPaths::GameDir() or FPaths::GameUserDir()?

Anyway, I would probably save profile specific information there or in the Game Config and load it into a custom game instance.
However, this is only useful if you’re only allowing one profile.

I have seen that functions. GameUserDir() returns subdir inside Saved/. Where is the Saved directory if I installed my game to “Program files/MyGame” or to sdcard1/Android/MyGame? Which directory I need to use if I want to create many userprofile files and save large binary files?

Well, really, Unreal doesn’t have a “specific” folder for this stuff like Unity does. Atleast not that I know of. So, it is up for you to decide where you’d like to put it. That is why I recommended the two FPaths functions which seem to be the most reasonable as both of those functions look at FApp::GetGameName() to find the game’s installation directory. So, more than likely FPaths::GameDir() returns “Program Files/My Game/” or “sdcard1/Android/My Game/” and it seems FPaths::GameUserDir() returns the same thing as FPaths::GameDir() for me.

Here is just a suggestion:
Use FPaths::GameSavedDir() and create subfolders inside of it. One for profiles and one for the binary data.
It should end up looking like: “Program Files/My Game/Saved/Profiles” or “sdcard1/Android/My Game/Saved/Profiles” and “Program Files/My Game/Saved/Binary_Data” or “sdcard1/Android/My Game/Saved/Binary_Data” you’d then just have to load it as you see fit.

Look at the USaveGame class and UGameplayStatics::SaveGameToFile()