Config not being saved (UPROPERTY, UCLASS)

Hi !

I’m trying to save some variables into a config file, i followed every solution i found on the web/answerhub but nothing seems to work.

I checked in /Config and /Saved/Config/[Platform]/ no variable is saved, i tried UCLASS(Config=) with uppercase and lowercase (i saw both on example) and of course other config file name.
And i tried to delete the /Saved folder and even multiple full project recompilation (by deleting /Saved /Intermediate/ /Binary

Where am i wrong ?

UCLASS(config=Game, defaultconfig)
class CONFIGTEST_API AMyPlayerController : public APlayerController
{
	GENERATED_BODY()

public:
	AMyPlayerController();

	void Tick(float DeltaTime) override;


public:

	UPROPERTY(config, EditAnywhere, BlueprintReadWrite)
	FString		superVar = "OK";

	UPROPERTY(config)
	float		superFloat = 999;
};

Thanks in advance !

You shouldn’t be assigning values to the config variables within the header file.

You can set default values within the appropriate default config file (in your instance “GAMENAMEGame.ini”, wherein “GAMENAME” is the actual name of your game) under a heading for your class holding the config variables:

[/Script/GAMEMODULE.AMyPlayerController]
superVar=OK
superFloat=999.0f

Replace “GAMEMODULE” with the actual name of your game module.

Then, when you run your game, the values within the config file will be read into the variables in your class.

The values will only appear within the “/Saved/Config/[Platform]/” files if they are different from the default; otherwise the default values will be used.

In order to modify them, you have to use the GConfig global:

if (GConfig)
{
    GConfig->SetString(
        TEXT("/Script/GAMEMODULE.AMyPlayerController"),
        TEXT("superVar"),
        TEXT("Not Okay"),
        GGameIni
    );
    GConfig->SetFloat(
        TEXT("/Script/GAMEMODULE.AMyPlayerController"),
        TEXT("superFloat"),
        100.0f,
        GGameIni
    );
}

GConfig also has SetText, SetInt, SetBool, SetColor, SetVector, and SetRotator to store other types of variables.
FName values should be converted to FString when storing.

The section and key arguments can be held in an FString, but will need to be passed in via pointer. Values are passed normally:

FString Section = TEXT("/Script/GAMEMODULE.AMyPlayerController");
FString Key = TEXT("superVar");
FString Value = TEXT("Not Okay");

if (GConfig)
{
    GConfig->SetString(*Section, *Key, Value, GGameIni);
}

After setting the values, you need to flush the config file to ensure they are applied:

if (GConfig)
{
    GConfig->Flush(false, GGameIni);
}

Or, from within your UCLASS(Config) object you can call “SaveConfig();”

You can also read from config files into any variable, without using the UCLASS and UPROPERTY specifiers:

FString ValueReceived;
if (GConfig)
{
    GConfig->GetString(
        TEXT("/Script/GAMEMODULE.AMyPlayerController"),
        TEXT("superVar"),
        ValueReceived,
        GGameIni
    );
}

A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums,Read%26_Write_to_Config_Files

Okay i will try that.

Thanks a lot, very explicit !