User config files

Config files are fully editable by user, usually they are form of .INI. They can allow user to override specific values if they for example want a huge drawing distance or just want to mess with some game values for fun. Many AAA RPG-titles have a config files, Bethesda games are good examples. There is a Config Section in Property Specifiers where you can see how to get Config files to load data to variables on project startup. In this example, we are going to create a custom Config file with a custom headers and load that data on runtime.

How to create and load a file

Start by creating a C++ class based on Actor. Add Scene component to root and Staticmesh component. Create Blueprint class based on your C++ class and inside blueprint class set something for the mesh, just for visuals. After your class is ready we can start creating the config file.

First thing to do is to set the Class Specifier. By giving a Class Specifier ”config = MySettings”, we are giving this class right to write into a config file. In this particular case, we are creating our own INI file called MySettings. Config needs it’s own include ”ConfigCacheIni.h”, so remember to add that also.

#include "ConfigCacheIni.h"
UCLASS(config = MySettings)

After this, close and open your game and you will have a MySettings.ini file in folder ”SavedConfigWindows” folder. There are lot of other configuration files there also. Now, open your config file and add header section and some variables under it.

[Player Settings]

After that. Let’s go back to edit C++. Inside our Class BeginPlay(), we write our code.

// Create some variables.
FString Name;
bool ToggleCrouch;
float Sensitivity;
// Get path to our settings file.
FString GameSettingsFile = FPaths::GeneratedConfigDir().Append(TEXT("Windows/MySettings.ini"));
// Load Files. First parameters are the Header and variable in our .INI file. Second parameter is our variable where to pass the data. Final parameter is the path to our file. 
GConfig->GetString(TEXT("Player Settings"), TEXT("Name"), Name, GameSettingsFile); 
GConfig->GetBool(TEXT("Player Settings"), TEXT("ToggleCrouch"), ToggleCrouch, GameSettingsFile); 
GConfig->GetFloat(TEXT("Player Settings"), TEXT("Sensitivity"), Sensitivity, GameSettingsFile);
// Print our results. 
UE_LOG(LogTemp, Warning, TEXT("%s"), *Name); 
UE_LOG(LogTemp, Warning, TEXT("%i"), ToggleCrouch); 
UE_LOG(LogTemp, Warning, TEXT("%f"), Sensitivity);

Next we will Write into file.

// Section name. We can create new or use the old ones.
FString Section = "Player Settings";
// First parameter is the section header. Second parameter is the variable. Third parameter is the variable you want to insert to file. Last parameter is the path to file.
GConfig->SetString(*Section, TEXT("name"), TEXT("John!"), GameSettingsFile);
// Remember to flush. Otherwise the save may fail!
GConfig->Flush(false, GameSettingsFile);

Thats it! You now know how to read and write configuration files that are freely editable by users.




Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *