How to store variables to a custom .ini file?

Hey guys I’ve been using UE4 C++ for the past 6 months and I still don’t know exactly how to do this. I am trying to create config variables for user gameplay preferences and graphics settings that would be stored on a custom .ini file.
This is what I have now,

UCLASS(config=UserSettings)
class UQuantumUserSettings : public UGameUserSettings
{
	GENERATED_BODY()
	
public:

	/********************/
	/* Gameplay Settings*/
	/********************/

	UPROPERTY(Config)
	bool ToggleSprint;

	UPROPERTY(Config)
	bool ToggleCrouch;
};

This complies with no problems and it generates an .ini file called UserSettings, but the file is blank. Am I on the right track? Can someone maybe show me how to do this correctly?

That didn’t work and im doing c++ btw

If the file was created, that’s a very good sign.

Have you tried adding your settings to see whether it makes a difference?

At least for Blueprint classes, the syntax is

[/Game/CONTENTPATH/CLASSNAME.CLASSNAME_C]
VARIABLE = SETTING

… where CONTENTPATH is the path within the Content directory.

In the below example, I’ve added a configurabe variable to the class TestActor, which lies in PROJECTPATH/Content/TopDownBP/Blueprints:

[/Game/TopDownBP/Blueprints/TestActor.TestActor_C]
TestVarEditable=201

If that doesn’t work, try leaving off the “_C” syntax. I think that might be Blueprint-only.

Did you see this: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums ?
Especially the safe path: YourGame\Saved\Config\Windows .

I’ve read that many times but it doesn’t show how to write to a custom ini file. I want my variables to be in a separate ini file not with the defaultgame.ini.

This is an old question but I was having the same problem and happened across the solution, so I figured I’d post it for posterity.
Here’s how it works:

Your c++ class definition should look like this.

UCLASS(config=NewConfigFileName)
class YOURGAME_API AExample : public AActor
{
    GENERATED_BODY()

    UPROPERTY(Config)
          int32 test_number;
    UPROPERTY(Config)
          FString test_string;
}

Edit: Oh and I almost forgot, you can have other properties in your UPROPERTY declaration.

As you noted the UCLASS declaration will automatically create an empty .ini file for you at compile time. Open up that file and type this inside it:

[/Script/YourGame.Example]
test_number=1
test_string=hi

Note how I typed Example in the config file and not AExample. Now whatever values you assigned in the ini file will automatically be loaded into your c++ variables. However, if you want to make a change to the ini file you’ll have to completely close out and reload the editor. I haven’t yet found a way to reload values on the fly and I’m not sure there is one. You can find more info in the documentation here: Configuration Files | Unreal Engine Documentation

Thank you! This helped me a ton.

One thing to note is you have to add Default to the start of the config file if you want a default one in Config/

To make this usable in a Blueprint I had to slightly alter your code to be more like this:

UCLASS(config=NewConfigFileName, Blueprintable, BlueprintType)
 class YOURGAME_API AExample : public AActor
 {
     GENERATED_BODY()
 
public:
     UPROPERTY(Config, BlueprintReadOnly)
           int32 test_number;
     UPROPERTY(Config, BlueprintReadOnly)
           FString test_string;
 }

Hi guys, I have a question about this:

consider that I have my project cooked with some default settings, but on a specific machine I want to change them.
How can I override the values in .ini that has been packaged?

Thank you

There’s an importance part here… the section name must be [/Script/<project_name>.<class_name>]. I spent too much on this today.

For writing to custom ini file, instead of using one of the globals as file name (GGameIni etc), you need to pass in your file’s absolute path. For instance, if I want to save data in an ini called DefaultMyGame.ini stored in [Project]/Config, I’ll use as path FPaths::ProjectConfigDir() / TEXT("DefaultMyGame.ini").

And for the section, it’s as others said: /Script/YourProjectName.ClassName
If it is a plugin, it’ll be /Script/ModuleName.ClassName