Reading and writing to config files in UE

Configuration files are a nice way to store various game variables in Unreal Engine games. They are particularly good in that you can make changes to parameters in them and not have to compile/build you game again. Your power users could even tweak things by making changes to these parameters.

Unreal comes with a bunch of configuration files that you can find under [Game Location]\Saved\Config\Windows

Below is a screenshot indicating the config files I have by default on creating a new blank project

configuration files

Reading from config files:

You can read any parameter from these files by making use of GConfig as shown below:


 if(!GConfig) return;

 FString value;

 GConfig->GetString(TEXT("/Script/UnrealEd.LevelEditorPlaySettings"),
 TEXT("MouseControlLabelPosition"),
 value,
 GEditorPerProjectIni);

 UE_LOG(LogTemp, Log, TEXT("%s"),*value);

In this example we intend to read the MouseControlLabelPosition value under the catergory /Script/UnrealEd.LevelEditorPlaySettings from the file EditorPerProjectUserSettings.ini.

We call the GeString method in this case because the target value is a string, you have other options such as GetBool etc. The first argument you pass in is the section name. Take a look at the screenshot below showing what the file contents look like. The second argument is the name of the parameter you want to read. The third argument is the variable you want to store the result into, note we have made this an FString. The fourth argument is a const for the file we want to read in this case for EditorPerProjectUserSettings.ini it is GEditorPerProjectIni.

configuration file sample.png

Writing to config files:

In the example below we will write to the config file Game.ini and create a new section called Sample.Section.


if (!GConfig) return;

FString newSection = "Sample.Section";
GConfig->SetString(
*newSection,
TEXT("PlayerName"),
TEXT("Chloe"),
GGameIni
);
GConfig->Flush(false,GGameIni);

Note that on completion we called GConfig->Flush. This is to ensure that any data held in buffers is flushed to the target config file.