Config Files, Read & Write to Config Files
In this tutorial I am showing you working code samples you can plug into your player controller (or any class if you remove the ClientMessage) Examples are shown for creating, reading and writing to config files using automatic method and also the GConfig
Overview
Author: ()
Dear Community,
In this tutorial I am showing you working code samples you can plug into your player controller class (or any class if you remove the ClientMessage() parts).
In these samples I retrieve various bits of information, including the Near Clip Pane value, from the Game and Engine inis, and I also write a new section into the Game.ini called Victory.Core and store some data there.
The main advantage of config files is that the user can go in and edit the data in a human-readible format any time they want!
How to create a new config file:
Suppose you have an Unreal project named YourGame and you want to have a config file named MyConfig thats going to be accessible by a class named TestActor.
Create a file named DefaultMyConfig.ini
in YourGame\Config
directory and write in it:
[SectionsToSave]
+Section=/Script/YourGame.TestActor
[/Script/YourGame.TestActor]
test_number=1
Each key-value should belong to a [section]. By default sections are read only. To be able to write to them the section name should be added to [SectionsToSave]
.
Alternatively you can add all the sections in the file at once to [SectionsToSave]
instead of adding them one by one:
[SectionsToSave]
bCanSaveAllSections=true
[/Script/YourGame.TestActor]
test_number=1
Please note that when you write out info to config files (from C++), the data is not stored in YourGame\Config
where the defaultconfig files are.
Instead (in either editor or packaged game) your data will be written to:
YourGame\Saved\Config\Windows
Here's were you can edit the data outside your game and then launch your game and read that changed data into your game system :)
Enjoy!
method 1
Automatic
This is our example class that's gonna have access to our config file:
UCLASS(Config = MyConfig/* name of the config file */)
class YourGame_API ATestActor : public AActor
{
GENERATED_BODY()
UPROPERTY(Config) /* mark variables that you want to write to config file */
int32 test_number;
};
Reading From Config File
test_number will automatically receive value from YourGame\Saved\Config\Windows\MyConfig.ini
If there is no MyConfig.ini file, test_number will be read from YourGame\Config\DefaultMyConfig.ini
void ATestActor::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Display, TEXT("test_number = %i"), test_number);
}
Writing to Config File
To write all UPROPERTY(Config)
variables to config file use the SaveConfig function:
void ATestActor::BeginPlay()
{
Super::BeginPlay();
test_number = 2;
SaveConfig(); // writes test_number to YourGame\Saved\Config\Windows\MyConfig.ini
}
If values are not saving, that's because in the config file, the value's section is not added to [SectionsToSave].
see example above.
method 2
Manual
ConfigCacheIni.h
Check out this header for all the function definitions and options available to you, for writing out arrays or reading in entire file as a FString, many, many useful functions
Code Samples
Quoting Solid Snake:
" There is a bunch of globals that you can use here to quickly grab the core configuration files:
- GEngineIni
- GInputIni
- GGameIni
- GGameUserSettingsIni
"
This is very useful info!
You will see in the functions below I rely on these global FStrings to more quickly write out the filename function parameter
Reading From Config File
//in your player controller class
void AVictoryController::VictoryConfigGetTests()
{
//Basic Syntax
/*
bool GetString(
const TCHAR* Section,
const TCHAR* Key,
FString& Value,
const FString& Filename
);
*/
if(!GConfig) return;
//~~
//Retrieve Default Game Type
FString ValueReceived;
GConfig->GetString(
TEXT("/Script/Engine.WorldInfo"),
TEXT("GlobalDefaultGameType"),
ValueReceived,
GGameIni
);
ClientMessage("GlobalDefaultGameType");
ClientMessage(ValueReceived);
//Retrieve Max Objects not considered by GC
int32 IntValueReceived = 0;
GConfig->GetInt(
TEXT("Core.System"),
TEXT("MaxObjectsNotConsideredByGC"),
IntValueReceived,
GEngineIni
);
ClientMessage("MaxObjectsNotConsideredByGC");
ClientMessage(FString::FromInt(IntValueReceived));
//Retrieve Near Clip Plane (how close things can get to camera)
float floatValueReceived = 0;
GConfig->GetFloat(
TEXT("/Script/Engine.Engine"),
TEXT("NearClipPlane"),
floatValueReceived,
GEngineIni
);
ClientMessage("NearClipPlane");
ClientMessage(FString::SanitizeFloat(floatValueReceived));
}
Writing to Config File
//write to existing Game.ini
//the results get stored in YourGameDir\Saved\Config\Windows
void AVictoryController::VictoryConfigSetTests()
{
if(!GConfig) return;
//~~
//New Section to Add
FString VictorySection = "Victory.Core";
//String
GConfig->SetString (
*VictorySection,
TEXT("RootDir"),
TEXT("E:\UE4\IsAwesome"),
GGameIni
);
//FColor
GConfig->SetColor (
*VictorySection,
TEXT("Red"),
FColor(255,0,0,255),
GGameIni
);
//FVector
GConfig->SetVector (
*VictorySection,
TEXT("PlayerStartLocation"),
FVector(0,0,512),
GGameIni
);
//FRotator
GConfig->SetRotator (
*VictorySection,
TEXT("SunRotation"),
FRotator(-90,0,0),
GGameIni
);
//ConfigCacheIni.h
//void Flush( bool Read, const FString& Filename=TEXT("") );
GConfig->Flush(false,GGameIni);
}
Of Great Importance
This line is very important
GConfig->Flush(false,GGameIni);
Sometimes the config file wont save changes if you don't call this function after you've set all your config keys.
Many thanks to Solid Snake for telling me about this, I might have spent an hour or two trying to figure out why my config file was only saving sometimes!
Conclusion
Now you know how to retrieve or edit any config file you want!
And you can even add new sections to existing config files!
Enjoy!
()