Download

UCLASS/UPROPERTY Configuration Not Reading

Hello UE4 forum,
I’ve been trying to get UCLASS/UPROPERTY configurations working for a bit and sadly have not gotten anywhere with it.

I have a class with UCLASS(Config = Game).
I have a declared variables as follows in the header file inside the class:
UPROPERTY(Config)
FString ConfigLoadName
and in DefaultGame.ini I have the property listed as such:
[/Script/ProjectName.ClassName]
ConfigLoadName=Default

Yet when I try to read this variable in the constructor it isn’t read from the config file. This is following all the info I could find in the documentation and online about it. Wondering what I’m missing here?

You need to handle the saving and loading of the configuration manually in the code.

The way I do it is I set up two standalone functions apart from the configuration class in the same file as the class. Replace “UGameConfig” with your class name. In the header, put:

UGameConfig* LoadGameConfig();
void SaveGameConfig(UGameConfig* GameConfig);

and in the implementation file, put:

UGameConfig* LoadGameConfig() {
        UGameConfig* Config = NewObject<UGameConfig>(GetTransientPackage(), UGameConfig::StaticClass());
        Config->LoadConfig(Config->GetClass(), *GetGameConfigFilename());

        return Config;
}

void SaveGameConfig(UGameConfig* GameConfig) {
        GameConfig->SaveConfig(CPF_Config, *GetGameConfigFilename());
}

Hope this helps!

Thank you for the reply, that makes sense. Unfortunately though it’s still not reading the configuration file - would this be a correct way to load the DefaultGame.ini config? LoadConfig(GetClass(), TEXT("/Config/DefaultGame.ini"));

Sorry, I forgot this part:

.h

FString GetGameConfigFilename();

.cpp

FString GetGameConfigFilename() {
        return FPaths::Combine(*FPaths::ProjectSavedDir(), TEXT("Config"), TEXT("GameSettings.ini"));
}

Why do you need to piggyback off of DefaultGame.ini? My setup will use a new custom config file located at Saved/Config/GameSettings.ini.

I’m trying to get it to load a config file at all, and figured the DefaultGame.ini would be the easiest and most straightforward to access since it’s used as an example with the UCLASS/UPROPERTY configuration documentation.

Apologies, but I’m not really understanding why we need to get a pointer to load the configuration file? Couldn’t I run LoadConfig(GetClass(), *GetGameConfigFilename()) straight in the constructor?

It probably works, I’ve just never tried it. I’m just sharing what I know. :crazy_face:

Ok, got it to read using the following:
LoadConfig(GetClass(),*FPaths::Combine(*FPaths::ProjectSavedDir(), TEXT("Config"), TEXT("GameSettings.ini")));

And had to create Saved/Config./GameSettings.ini to match the filepath, adding the configuration stuff there.

Ty for the help :slight_smile:

BTW I use the GetGameConfigFilename function just so I don’t need to change it in multiple places (saving, loading, etc.).