GConfig->GetString Always Returns Value of Last Line Even if It Does Not Exist

Hi,

I have a function that uses GConfig to read a string from a config file. The function looks like this:

	FString ConfigFile = FPaths::ProjectDir().Append(*FileName).Append(".ini");

	GConfig->GetString(
		*Section,
		*Key,
		Value,
		ConfigFile
	);

	GConfig->Flush(false, ConfigFile);

I pass the config file name to it, and it appends .ini to the file name and adds the project directory in front of the whole thing. For example, I pass it MyFile and it’ll turn it into C:\ProjectDir\MyFile.ini

Anyway, I have a for loop that runs the function to read the following keys from the config file: key1, key2, key3.

The config file looks like this:

key1=One
key2=Two

And the function returns:

One
Two
Two

As you may have noticed, key3 does not exist in the config file but the function always returns the last value it successfully read anyway, in this case, Two. If I were to change the config file to:

key1=One

It would return

One
One
One

Is this a bug or am I doing something wrong? :confounded:

I’m using UE 4.27.2, but I assume this is also the case in UE5.

Thanks :wink:

Is there any chance the Value variable is not getting reset and you are doing this in a for-loop? That would explain the behavior you are describing.

Can you share the rest of your code to confirm this?

I am doing it in a for-loop. I’ve made a blueprint function out of it, here is an example:

I’m feeding a string array of Key1, Key2, Key3 into the for loop, the function reads those keys from the config file and prints whatever value they have.

The config looks like this:

[Section1]

Key1=One
Key2=Two

Key3 is missing from the config but the function will still return Two, so it does seem that the Value variable is not being cleared in the GetString function. :thinking:

I managed to fix the issue by clearing the Value variable before the GetString function is run like this:

	FString ConfigFile = FPaths::ProjectDir().Append(*FileName).Append(".ini");
	Value = "";

	GConfig->GetString(
		*Section,
		*Key,
		Value,
		ConfigFile
	);
1 Like

Awesome thanks for confirming, that was indeed the exact problem I was suspecting.

I’m glad you got it fixed.

1 Like