Save File Paths
In this article, we will explore options you have for saving files to disc.
Please note that saving files on Windows requires a different path than saving files on Mac or Linux.
First, it's important to note that Monogame has removed the storage class. See this discussion:
https://github.com/MonoGame/MonoGame/issues/4311
So, if you're migrating from Xna to Monogame, the storage class wont be available to use.
You'll need to handle it yourself, which can vary widely based on platform.
Let's take a look at save file paths on Windows, Mac, and Linux.
Saving Files To Application Folder (Windows)
There are many options for saving files on Windows, but there are hidden pitfalls as well.First, lets take a look at the naive way of saving files to the game's application directory:
You may think this is a working solution, but it isn't! Some windows users wont have permissions
to write to this directory. This means they won't be able to save their game! So, we must
consider alternative locations to save data to.
Saving Files To AppData (Windows)
Windows applications often store their data and settings in an AppData folder, and each Windowsuser account has its own. It’s a hidden folder, so you’ll only see it if you show hidden files
in the file manager. Each user account has its own AppData folder with its own contents. This
allows Windows programs to store multiple sets of settings if a computer is used by multiple people.
The AppData folder was introduced on Windows Vista, and is still in use on Windows 10, 8, and 7 today.
This will target the directory: //C:\Users\UserName\AppData\Roaming\MyGame\
This is the suggested way of storing files on Windows:
You may need to store data for ALL users. In that case, you need to look into using CommonApplicationData:
This will target the directory: //C:\ProgramData\MyGame\
You can also choose to store save data in LocalApplicationData:
This will target the directory: //C:\Users\UserName\AppData\Local\MyGame\
You may want to save game files to the MyDocuments folder. Alright, buckle up.
This will target the directory: //C:\Users\UserName\Documents\MyGame\
Please note that on Windows 8.1, due to the addition of SkyDrive, this path will return
"C:\Users\Administrator\SkyDrive\Documents\MyGame\" - fascinating.
Also note that Window's Updates will occasionally reset some random settings, which may alter this path.
Saving Files For Mac
On Mac OSX, the save file path needs to look something like this:"/Users/bob/Library/Application Support/MyGame/"
Note that there is a space in Application Support. That's important. In order to get this path, use this code:
Saving Files For Linux
There are many possible configurations of Linux and supporting all of them may not be possible.So, assuming you absolutely must do this, here's the path you'll (probably) need:
"/home/bob/.local/share/MyGame/"
Implementing this path is left up to you. Here's a starting point: "~/.local/share/MyGame"
Have fun.
What about SDL_GetPrefPath?
Monogame doesn't use SDL for the DirectX templates (as of 2021), so this isn't available for this project.For the OpenGL template, sure, use SDL_GetPrefPath. Here's a link to what it will return: https://wiki.libsdl.org/SDL_GetPrefPath
What about Proton? Or Console platforms?
Console savepaths are implemented specifically, and you'll need to be a registered developer to get acess to thesamples and examples that will guide you through this process. Good luck.